I'm trying to upgrade a class to use GuzzleHttp\Client
to search tweets using twitter api. I'm having trouble attaching Oauth1
. It worked fine with Guzzle3 and OAuthPlugin.
Here is the code block:
$client = new Client(['base_uri' => 'https://api.twitter.com']);
$auth = new Oauth1([
'consumer_key' => Config::get('twitter.consumer_key'),
'consumer_secret' => Config::get('twitter.consumer_secret'),
'token' => Config::get('twitter.token'),
'token_secret' => Config::get('twitter.token_secret')
]);
// Not sure if this is correct
$client->getEmitter()->attach($auth); // This is line 40 inside TwitterServiceProvider.php
I get the following error:
InvalidArgumentException in Client.php line 80: Magic request methods require a URI and optional options array
1. in Client.php line 80
2. at Client->__call('getEmitter', array()) in TwitterServiceProvider.php line 40
P.S So far, I have figured, I should be using https://github.com/guzzle/oauth-subscriber. However, no luck yet.
Resolved.
guzzle/oauth-subscriber
and it works now. There is a good example here: https://github.com/guzzle/oauth-subscriber
base_uri
options should have a trailing slash.New Code:
$stack = HandlerStack::create();
$auth = new Oauth1([
'consumer_key' => Config::get('twitter.consumer_key'),
'consumer_secret' => Config::get('twitter.consumer_secret'),
'token' => Config::get('twitter.token'),
'token_secret' => Config::get('twitter.token_secret')
]);
$stack->push($auth);
$client = new Client([
'base_uri' => 'https://api.twitter.com/1.1/',
'handler' => $stack,
'auth' => 'oauth'
]);
A request is made like below:
$client->get('search/tweets.json', [
'query' => ['q' => $query]
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With