Has anyone used the OAuth extension for PHP?


#1

I’m having an issue with the PECL OAuth extension for PHP. My code is below:

define("TWITTER_CONSUMER_KEY", "consumer-key");
define("TWITTER_CONSUMER_SECRET", "consumer-secret");
$oauth = new OAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
$oauth->setToken('valid-token', 'valid-key');
var_dump($oauth->fetch('https://api.twitter.com/account/verify_credentials.json'));

Whenever I load the page this code is on, instead of producing output in my browser, my browser attempts to download a blank file with the same name as the PHP script. Anyone have any ideas? Has anyone successfully used the PECL OAuth extension with twitter?


#2

I think thats because it is a json file. My browser also starts to download json files.


#3

I’d agree with you, except for the fact that I’m not loading that URL in my browser.

I experience the same behavior if I try to fetch the Google homepage as well…


#4
  1. You need to use the getLastResponse() method to get the response of your request.
  2. There is also probably a Content-Type issue here. Let’s be sure your web-server is returning the Content-Type: text/html header by using the PHP header() function.

This code is implementing these two suggestions. Try it and let us know how it goes:

header('Content-Type: text/html', true);

define(“TWITTER_CONSUMER_KEY”, “consumer-key”);
define(“TWITTER_CONSUMER_SECRET”, “consumer-secret”);
$oauth = new OAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
$oauth->setToken(‘valid-token’, ‘valid-key’);
$oauth->fetch(‘https://api.twitter.com/1/account/verify_credentials.json’);

$result = json_decode($oauth->getLastResponse());
var_dump($result);


#5

header(‘Content-Type: text/html’, true);

$oauth->setToken('valid-token', 'valid-key');
$oauth->fetch('https://api.twitter.com/1/account/verify_credentials.json');
$response = json_decode($oauth->getLastResponse());

$response_info = $oauth->getLastResponseInfo();
echo var_dump($response);

That’s the code I have now… the define()s are actually further up in my real file, as they are used in more than one place. Unfortunately, I’m still having the same issue.

P.S. not seeing how to do code formatting on here… :wink: