我正在尝试升级类以使用{ap}来使用GuzzleHttp\Client
来搜索推文。我无法附上Oauth1
。它适用于Guzzle3和OAuthPlugin。
以下是代码块:
$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
我收到以下错误:
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到目前为止,我已经想过,我应该使用https://github.com/guzzle/oauth-subscriber。但是,没有运气了。
答案 0 :(得分:5)
解决。
guzzle/oauth-subscriber
,现在就可以了。这里有一个很好的例子:https://github.com/guzzle/oauth-subscriber base_uri
选项应该有一个斜杠。新守则:
$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'
]);
请求如下:
$client->get('search/tweets.json', [
'query' => ['q' => $query]
]);