2017-05-06 97 views
0

我想使用Twitter Streaming Api和過濾器查詢。
而繼GuzzleHttp文檔,我訪問這樣的:需要Twitter上的授權Stream Api使用Guzzle PHP

<?php 

use GuzzleHttp\Client; 
use GuzzleHttp\HandlerStack; 
use GuzzleHttp\Subscriber\Oauth\Oauth1; 

$stack = HandlerStack::create(); 

$middleware = new Oauth1([ 
    'consumer_key' => 'my_key', 
    'consumer_secret' => 'my_secret', 
    'token'   => 'my_token', 
    'token_secret' => 'my_token_secret' 
]); 
$stack->push($middleware); 

$client = new Client([ 
    'base_uri' => 'https://stream.twitter.com/1.1/', 
    'handler' => $stack, 
    'auth' => 'oauth' 
]); 

$client->post('statuses/filter.json', [ 
    'form_params' => [ 
     'track' => 'some_word_to_track', 
    ], 
]); 

但執行此代碼時,我發現了以下異常:

PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: 
Client error: 'POST https://stream.twitter.com/statuses/filter.json' 
resulted in a '401 Authorization Required' response 
in /Users/.../vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111 


結論:

  • 是因爲我無法使用GuzzleHttp的Oauth1訂閱 Twitter流媒體API?
  • 還是因爲我缺少 身份驗證的內容?

回答

0

看來你不包括'auth' => 'oauth'選項,您的要求,如圖the docs

// Set the "auth" request option to "oauth" to sign using oauth 
$res = $client->get('statuses/home_timeline.json', ['auth' => 'oauth']);