Twitter搜索API v1.1仅适用于应用程序
问题描述:
我已根据文档为应用程序唯一身份验证准备了不记名令牌,现在我有我的标题,但我不知道如何发送它以获取响应:Twitter搜索API v1.1仅适用于应用程序
<?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";
通常情况下,我只需要做一个$ result = json_decode(file_get_contents($ url),true),但我不知道如何在这种情况下继续。
答
你可以玩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));