OAuth PHP code error?

OAuth PHP code error?

Page 1 of 1

1 Replies - 2703 Views - Last Post: 21 July 2009 - 07:16 AM Rate Topic: -----

#1 partykey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-July 09

OAuth PHP code error?

Post icon  Posted 21 July 2009 - 02:41 AM

I'm testing out php code to login using Twitter OAuth. The following is the "start.php" file:
<?php
echo '<a href="' . $twitterObj->getAuthenticateUrl() . '">Authorize with Twitter</a>';
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);

echo '<a href="' . $twitterObj->getAuthenticateUrl() . '">Authorize with Twitter</a>';
?>

Here is the "secret.php" file (where I have replaced "xxx" with our respective consumer_key and consumer_secret values ):

<?php
$consumer_key = 'xxx';
$consumer_secret = 'xxx';
?>

And here is the EpiTwitter.php file:

<?php
/*
 *  Class to integrate with Twitter's API.
 *	Authenticated calls are done using OAuth and require access tokens for a user.
 *	API calls which do not require authentication do not require tokens (i.e. search/trends)
 */
class EpiTwitter extends EpiOAuth
{
  const EPITWITTER_SIGNATURE_METHOD = 'HMAC-SHA1';
  protected $requestTokenUrl= 'http://twitter.com/oauth/request_token';
  protected $accessTokenUrl = 'http://twitter.com/oauth/access_token';
  protected $authorizeUrl   = 'http://twitter.com/oauth/authorize';
  protected $authenticateUrl= 'http://twitter.com/oauth/authenticate';
  protected $apiUrl		 = 'http://twitter.com';
  protected $searchUrl	  = 'http://search.twitter.com';

  public function __call($name, $params = null)
  {
	$parts  = explode('_', $name);
	$method = strtoupper(array_shift($parts));
	$parts  = implode('_', $parts);
	$path   = '/' . preg_replace('/[A-Z]|[0-9]+/e', "'/'.strtolower('\\0')", $parts) . '.json';
	if(!empty($params))
	  $args = array_shift($params);

	// intercept calls to the search api
	if(preg_match('/^(search|trends)/', $parts))
	{
	  $query = isset($args) ? http_build_query($args) : '';
	  $url = "{$this->searchUrl}{$path}?{$query}";
	  $ch = curl_init($url);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	  return new EpiTwitterJson(EpiCurl::getInstance()->addCurl($ch));
	}

	return new EpiTwitterJson(call_user_func(array($this, 'httpRequest'), $method, "{$this->apiUrl}{$path}", $args));
  }

  public function __construct($consumerKey = null, $consumerSecret = null, $oauthToken = null, $oauthTokenSecret = null)
  {
	parent::__construct($consumerKey, $consumerSecret, self::EPITWITTER_SIGNATURE_METHOD);
	$this->setToken($oauthToken, $oauthTokenSecret);
  }
}

class EpiTwitterJson implements ArrayAccess, Countable,  IteratorAggregate
{
  private $__resp;
  public function __construct($response)
  {
	$this->__resp = $response;
  }

  // Implementation of the IteratorAggregate::getIterator() to support foreach ($this as $...)
  public function getIterator ()
  {
	return new ArrayIterator($this->__obj);
  }

  // Implementation of Countable::count() to support count($this)
  public function count ()
  {
	return count($this->__obj);
  }
  
  // Next four functions are to support ArrayAccess interface
  // 1
  public function offsetSet($offset, $value) 
  {
	$this->response[$offset] = $value;
  }

  // 2
  public function offsetExists($offset) 
  {
	return isset($this->response[$offset]);
  }
  
  // 3
  public function offsetUnset($offset) 
  {
	unset($this->response[$offset]);
  }

  // 4
  public function offsetGet($offset) 
  {
	return isset($this->response[$offset]) ? $this->response[$offset] : null;
  }

  public function __get($name)
  {
	if($this->__resp->code != 200)
	  EpiOAuthException::raise($this->__resp->data, $this->__resp->code);

	$this->responseText = $this->__resp->data;
	$this->response	 = json_decode($this->responseText, 1);
	$this->__obj		= json_decode($this->responseText);
	foreach($this->__obj as $k => $v)
	{
	  $this->$k = $v;
	}

	return $this->$name;
  }

  public function __isset($name)
  {
	$value = self::__get($name);
	return empty($name);
  }
}


I am using PHP version 5.1.6 and all files have been uploaded to our website domain. The problem is that when I load the "start.php" in a browser under our domain, only a blank page appears (instead of a visible link to authorize with Twitter).

I'd appreciate any ideas as to why I'm getting a blank page.

This post has been edited by Jayman: 21 July 2009 - 06:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: OAuth PHP code error?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2927
  • View blog
  • Posts: 10,114
  • Joined: 08-August 08

Re: OAuth PHP code error?

Posted 21 July 2009 - 07:16 AM

1.) This function: getAuthenticateUrl does not exist in your class definition.

2.) I have no idea what happens to $consumer_key and $consumer_secret in this instantiation:
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
I would do it this way:
$twitterObj = new EpiTwitter;
$twitterObj->some_variable = $consumer_key;
$twitterObj->some_other_variable = $consumer_secret;
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1