Twitter搜索API v1.1,仅支持应用程序身份验证


Twitter Search API v1.1 with app only authentication

我已经为应用程序准备了我的承载令牌,根据文档,我现在有我的头,但我不知道如何发送它来获得响应:

      <?php
                $headers = array( 
                    "GET /1.1/search/tweets.json? HTTP/1.1", 
                    "Host: api.twitter.com", 
                    "User-Agent: my Twitter App v.1",
                    "Authorization: Bearer mybearertoken",

                ); 
$url = "https://api.twitter.com/1.1/search/tweets.json?q=%23SOMEHASHTAG&count=100";

通常情况下,与v1,我只是做一个$result=json_decode(file_get_contents($url),真),但我不知道如何在这种情况下进行。

您可以使用PHP的上下文参数。更多信息可以在这里找到:http://docs.php.net/context

在你的例子中,代码应该是这样的:

$headers = array( 
            "GET /1.1/search/tweets.json? HTTP/1.1", 
             "Host: api.twitter.com", 
             "User-Agent: my Twitter App v.1",
             "Authorization: Bearer mybearertoken",
             ); 
$url = "https://api.twitter.com/1.1/search/tweets.json?q=%23SOMEHASHTAG&count=100";
$context = stream_context_create($headers);
$result=json_decode(file_get_contents($url, false, $context));