做你想做的事,你必须:
(可选地,您可以添加第 0 步,即尝试正常提交推文,只有在出现错误时才继续执行其他步骤,这取决于您。)
在这里,您有一个代码可以发出这些请求并解释搜索的 json 等:
$settings = array(
'oauth_access_token' => "...",
'oauth_access_token_secret' => "...",
'consumer_key' => "...",
'consumer_secret' => "..."
);
$API = new twitter_API($settings);
$tweet_text = '>>testing the twitter API-1.1...';
## search the list of tweets for a duplicate...
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$json = $API->make_request($url, "GET");
$twitter_data = json_decode($json);
$id_str = null;
foreach ($twitter_data as $item){
$cur_text = $item->text;
if (strcmp($cur_text, htmlspecialchars($tweet_text))==0){
$id_str = $item->id_str;
echo "found a duplicate tweet with the id: " . $id_str . "<br /><br />";
}
}
## remove the duplicate, if there is one...
if ($id_str){
$url = "https://api.twitter.com/1.1/statuses/destroy/" . $id_str . ".json";
$json = $API->make_request($url, "POST");
echo $json . '<br /><br />';
}
## post the tweet
$url = "https://api.twitter.com/1.1/statuses/update.json";
$postfields = array(
'status' => $tweet_text
);
$json = $API->make_request($url, "POST", $postfields);
echo $json . '<br /><br />';
此代码使用类 twitter_API,它改编自[ref.]中的答案。你可以使用这个类,或者用 twitter-async 的函数替换对其函数的调用。
class twitter_API
{
private $oauth_access_token;
private $oauth_access_token_secret;
private $consumer_key;
private $consumer_secret;
protected $oauth;
public function __construct(array $settings){
if (!in_array('curl', get_loaded_extensions())){
echo 'you need to install cURL!';
exit();
}
$this->oauth_access_token = $settings['oauth_access_token'];
$this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
$this->consumer_key = $settings['consumer_key'];
$this->consumer_secret = $settings['consumer_secret'];
}
function build_base_string($base_URI, $method, $params){
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method . "&" . rawurlencode($base_URI) . '&' . rawurlencode(implode('&', $r));
}
function build_authorization_header($oauth){
$r = 'authorization: oauth ';
$values = array();
foreach ($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
function make_request($url, $type, $args=null){
$this->oauth = array( 'oauth_consumer_key' => $this->consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $this->oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');
if (($type=="GET") && (!is_null($args))){
$getfields = str_replace('?', '', explode('&', $args));
foreach ($getfields as $field){
$field_strs = explode('=', $field);
$this->oauth[$field_strs[0]] = $field_strs[1];
}
}
$base_info = $this->build_base_string($url, $type, $this->oauth);
$composite_key = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$this->oauth['oauth_signature'] = $oauth_signature;
// make request
$header = array($this->build_authorization_header($this->oauth), 'expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
if ($type=="POST"){
if (is_null($args)){
$args = array();
}
$options[CURLOPT_POSTFIELDS] = $args;
}
else if (($type=="GET") && (!is_null($args))){
$options[CURLOPT_URL] .= $args;
}
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return $json;
}
}