我整天都在努力工作,没有任何效果。我已经尝试过这个脚本使用Twitter的库来获取PHP,但我没有运气。我浏览了Twitter文档和大量示例来了解我当前的代码。我没有得到bearer_token回来。
1)我已经设置了开发帐户。 2)我已经生成了密钥和令牌。
请帮忙。
<?php
include('config.php');
$hashtags="#Pistorious #OscarPistorious #PistoriousTrial";
$hashtags = str_replace("#", "%23", $hashtags);
$hashtags = str_replace(" ", "+", $hashtags);
$query=rawurlencode($hashtags);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$oauth_key = CONSUMER_KEY;
$oauth_nonce = substr(md5(rand()),0,32);
$oauth_timestamp = time();
$oauth_token = ACCESS_TOKEN;
$key = '';
$key .= rawurlencode(CONSUMER_SECRET);
$key .= '&';
$key .= rawurlencode(ACCESS_TOKEN_SECRET);
$oauth_hash = '';
$oauth_hash .= 'oauth_consumer_key="{$oauth_key}"&';
$oauth_hash .= 'oauth_nonce="{$oauth_nonce}"&';
$oauth_hash .= 'oauth_signature_method=HMAC-SHA1&';
$oauth_hash .= 'oauth_timestamp="{$oauth_timestamp}"&';
$oauth_hash .= 'oauth_token="{$oauth_token}"&';
$oauth_hash .= 'oauth_version=1.0';
$base = '';
$base .= 'POST';
$base .= '&';
$base .= rawurlencode($url);
$base .= '&';
$base .= rawurlencode($oauth_hash);
$base .= $query;
$oauth_signature = base64_encode(hash_hmac('sha1', $base, $key, true));
$oauth_header = '';
$oauth_header .= 'oauth_consumer_key="'.$oauth_key.'", ';
$oauth_header .= 'oauth_nonce="'.$oauth_nonce.'", ';
$oauth_header .= 'oauth_signature="'.$oauth_signature.'", ';
$oauth_header .= 'oauth_signature_method="HMAC-SHA1", ';
$oauth_header .= 'oauth_timestamp="'.$oauth_timestamp.'", ';
$oauth_header .= 'oauth_token="'.$oauth_token.'", ';
$oauth_header .= 'oauth_version="1.0", ';
$url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication
$headers = array(
'POST /oauth2/token HTTP/1.1',
'Host: api.twitter.com',
'User-Agent: my Twitter App v.1',
"Authorization: OAuth $oauth_header",
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length: 76',
'Connection:close',
'Accept:*/*'
);
$ch = curl_init(); // setup a curl
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
curl_setopt($ch, CURLOPT_POST, 1); // send as post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$retrievedhtml = curl_exec ($ch); // execute the curl
curl_close($ch); // close the curl
$output = explode("\n", $retrievedhtml);
$bearer_token = '';
foreach($output as $line) {
if($line === false)
{
// there was no bearer token
}else{
$bearer_token = $line;
}
}
if (false!=$bearer_token) {
$bearer_token = json_decode($bearer_token);
curl_close($ch);
$headers = array(
"GET /1.1/search/tweets.json".$url." HTTP/1.1",
"Host: api.twitter.com",
"User-Agent: testapp",
"Authorization: Bearer ".$token."",
);
$curl_request = curl_init();
curl_setopt($curl_request, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_URL, $formed_url);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($curl_request);
$err=curl_error($curl_request);
if ($err!="") {
echo curl_error($curl_request);
}
else {
curl_close($curl_request);
var_dump($response);
echo 'done';
}
} else {
echo 'didnt authenticate against server';
}
&GT;
答案 0 :(得分:0)
它现在有效。在我修改代码并使用TwitterAPIExchange / j7mbo之后。之后没有工作。