boz*_*doz 5 php twitter curl twitter-oauth
我已经创建了一个Twitter应用程序来自动发布到我的Twitter帐户.所以,我不需要授权新用户.
我已经设置了读/写访问级别,并收到了访问令牌.我在应用程序中使用了OAuth工具来生成cURL命令:
curl --request'POST''https : //api.twitter.com/1/statuses/update.json'-- data'status = Maybe + he%27ll + finally + find + his + keys.+%23peterfalk' --header'授权:OAuth oauth_consumer_key ="...",oauth_nonce ="97fad626790e8e5988d4a06cfd47fa74",oauth_signature ="...",oauth_signature_method ="HMAC-SHA1",oauth_timestamp ="1364161424",oauth_token ="......" ,oauth_version ="1.0"' - verbose
在命令之上,它说:
重要提示:此功能仅在几分钟内有效.还记得cURL命令实际上会执行请求.
我假设这将在Linux终端中工作.
我想知道如何将其转换为PHP cURL命令.这是我尝试过的.请注意,$ DST的值是https://dev.twitter.com/docs/auth/authorizing-request中"DST"的值; 它也等于--header 'Authorization:OAuth工具中cURL命令之后的字符串值.
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
data => 'status='.urlencode($status),
header => 'Authorization: '.$DST
)
));
$resp = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
但价值$resp是:
{
request: "/1/statuses/update.json",
error: "Could not authenticate you."
}
Run Code Online (Sandbox Code Playgroud)
有什么我做错了吗?请注意,OAuth工具表示cURL命令实际上可以正常工作.所以我认为这只是要弄清楚如何在PHP中安排cURL.我对它不是很熟悉.另请注意,如果可以的话,我想避免使用OAuth库.我觉得应该有一个更轻量级的解决方案,而不是安装一个完整的库.
我遇到了三个问题:
(1) 我没有将其包含在这个问题中,但我的签名创建不正确。
我所做的是,base64_encode(hash_hmac('sha1', $base, $key));但我应该将 hash_hmac 第四个参数设置为true返回原始二进制文件而不是十六进制(十六进制显示为 Twitter 文档中的示例,这让我感到困惑)。因此,正确的函数是:base64_encode(hash_hmac('sha1', $base, $key, true));。
(2) cURL 设置不正确。我需要 CURLOPT_HTTPHEADER 设置授权,并将 CURL_VERBOSE 设置为 true:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $DST));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status));
curl_setopt($ch, CURLOPT_URL, $url);
Run Code Online (Sandbox Code Playgroud)
(3) 如上面的代码所示,我必须将状态作为字符串而不是数组发布。我在这个问题上找到了这个问题的解决方案:Why can't I verify with OAuth? 。
所有这一切现在都运行良好。另外,请确保 Twitter 应用程序中的访问令牌表明访问级别为“读取和写入”。如果没有,请更改“设置”中的权限,然后返回“详细信息”并重新创建您的访问令牌。
<?php
class Tweet {
public $url = 'https://api.twitter.com/1/statuses/update.json';
function the_nonce(){
$nonce = base64_encode(uniqid());
$nonce = preg_replace('~[\W]~','',$nonce);
return $nonce;
}
function get_DST($status){
$url = $this->url;
$consumer_key = $your_consumer_key_here;
$nonce = $this->the_nonce();
$sig_method = 'HMAC-SHA1';
$timestamp = time();
$version = "1.0";
$token = $your_token_here;
$access_secret = $your_access_secret_here;
$consumer_secret = $your_consumer_secret_here;
$param_string = 'oauth_consumer_key='.$consumer_key.
'&oauth_nonce='.$nonce.
'&oauth_signature_method='.$sig_method.
'&oauth_timestamp='.$timestamp.
'&oauth_token='.$token.
'&oauth_version='.$version.
'&status='.rawurlencode($status);
$sig_base_string = 'POST&'.rawurlencode($url).'&'.rawurlencode($param_string);
$sig_key = rawurlencode($consumer_secret).'&'.rawurlencode($access_secret);
$tweet_sig = base64_encode(hash_hmac('sha1', $sig_base_string, $sig_key, true));
$DST = 'OAuth oauth_consumer_key="'.rawurlencode($consumer_key).'",'.
'oauth_nonce="'.rawurlencode($nonce).'",'.
'oauth_signature="'.rawurlencode($tweet_sig).'",'.
'oauth_signature_method="HMAC-SHA1",'.
'oauth_timestamp="'.rawurlencode($timestamp).'",'.
'oauth_token="'.rawurlencode($token).'",'.
'oauth_version="1.0"';
return $DST;
}
function set($status){
$url = $this->url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->get_DST($status)));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status));
curl_setopt($ch, CURLOPT_URL, $url);
$result = json_decode(curl_exec($ch));
if(!curl_errno($ch)){
$info = curl_getinfo($ch);
if($info['http_code']!='200'){
//error posting
echo 'Error: '.$result->{'error'};
}else{
//success
echo 'Success! <a href="https://twitter.com/AOKHalifax/status/'.$result->{'id_str'}.'" target="_blank">View Tweet</a>';
}
}else{
//error connecting
echo 'error posting';
}
curl_close($ch);
}
}
/*
Usage example:
$status = new Tweet();
$status->set('checking');
*/
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1778 次 |
| 最近记录: |