推特 API 媒体 ID / 状态更新
Twitter API with Media ID / Status update
我有以下代码,除了一个问题外,它运行良好,即将 media_id 附加到最终状态更新 post。媒体 ID 似乎被忽略,文本状态仅被上传。媒体上传请求或状态 post.
均未输出错误代码
有什么想法吗?
<?php
$oauth_access_token ="YOUR TOKEN";
$oauth_access_token_secret ="YOUR TOKEN SECRET";
$consumer_key ="YOUR KEY";
$consumer_secret ="YOUR SECRET";
//twitter api urls
$URLS=array(
"image"=>"https://upload.twitter.com/1.1/media/upload.json",
"status"=>"https://api.twitter.com/1.1/statuses/update.json");
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
function makeRequest($postfields,$url){
global $consumer_key;
global $consumer_secret;
global $oauth_access_token;
global $oauth_access_token_secret;
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$base_info = buildBaseString($url, 'POST', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return $json;
}
//Upload image to twitter and get media_id
$file = file_get_contents(BASEPATH . '/images/myphoto.png');
$postfields=array("media_data"=>base64_encode($file));
$result=makeRequest($postfields,$URLS['image']);
$imageresult=json_decode($result);
$imageid=$imageresult->media_id;
//output results
print_r($imageresult);
//Send status update with media_id attached
$postfields=array(
"status"=>"test messsage with image",
"media_ids"=>$imageid);
//output results
$result=makeRequest($postfields,$URLS['status']);
$statusresult=json_decode($result);
print_r($statusresult);
?>
媒体上传输出看起来完整
stdClass Object
(
[media_id] => 6.7933955975536E+17
[media_id_string] => 679339559755358208
[size] => 51719
[expires_after_secs] => 86400
[image] => stdClass Object
(
[image_type] => image/png
[w] => 187
[h] => 116
)
)
尝试使用 media_id_string 而不是 media_id
我发现问题出在 windows 主机上,它不喜欢 php 中的某些 curl 请求,因为它们并不总是有效。所以我将托管更改为 Linux,之后它运行良好。
我有以下代码,除了一个问题外,它运行良好,即将 media_id 附加到最终状态更新 post。媒体 ID 似乎被忽略,文本状态仅被上传。媒体上传请求或状态 post.
均未输出错误代码有什么想法吗?
<?php
$oauth_access_token ="YOUR TOKEN";
$oauth_access_token_secret ="YOUR TOKEN SECRET";
$consumer_key ="YOUR KEY";
$consumer_secret ="YOUR SECRET";
//twitter api urls
$URLS=array(
"image"=>"https://upload.twitter.com/1.1/media/upload.json",
"status"=>"https://api.twitter.com/1.1/statuses/update.json");
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
function makeRequest($postfields,$url){
global $consumer_key;
global $consumer_secret;
global $oauth_access_token;
global $oauth_access_token_secret;
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$base_info = buildBaseString($url, 'POST', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return $json;
}
//Upload image to twitter and get media_id
$file = file_get_contents(BASEPATH . '/images/myphoto.png');
$postfields=array("media_data"=>base64_encode($file));
$result=makeRequest($postfields,$URLS['image']);
$imageresult=json_decode($result);
$imageid=$imageresult->media_id;
//output results
print_r($imageresult);
//Send status update with media_id attached
$postfields=array(
"status"=>"test messsage with image",
"media_ids"=>$imageid);
//output results
$result=makeRequest($postfields,$URLS['status']);
$statusresult=json_decode($result);
print_r($statusresult);
?>
媒体上传输出看起来完整
stdClass Object
(
[media_id] => 6.7933955975536E+17
[media_id_string] => 679339559755358208
[size] => 51719
[expires_after_secs] => 86400
[image] => stdClass Object
(
[image_type] => image/png
[w] => 187
[h] => 116
)
)
尝试使用 media_id_string 而不是 media_id
我发现问题出在 windows 主机上,它不喜欢 php 中的某些 curl 请求,因为它们并不总是有效。所以我将托管更改为 Linux,之后它运行良好。