使用媒体发送状态

时间:2015-01-07 19:04:46

标签: php api curl twitter

我想使用twitter api向媒体发送状态。

我知道如何发送状态,但不知道状态+媒体。

我不想使用第三方课程。

要发布状态,我使用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;
}

$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');

if ($method == 'POST') {
    $oauth['status'] = $status;
}

$base_info = buildBaseString($url, $method, $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_VERBOSE => true,
                 CURLOPT_HEADER => false,
                 CURLOPT_URL => $url,
                 CURLOPT_RETURNTRANSFER => true,                
                 CURLOPT_FOLLOWLOCATION => false,
                 CURLOPT_SSL_VERIFYPEER => true,
                 CURLOPT_SSL_VERIFYHOST => 2,
                 CURLOPT_CAINFO => 'C:\xampp\php\cacert.pem');    
    if ($method == 'POST') {
    $options[CURLOPT_POST] = true;
    $options[CURLOPT_POSTFIELDS] = 'status='.rawurlencode($status);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);

这是我的新身份。

但是,发送状态+媒体需要更改的最小值是什么? 使用https://upload.twitter.com/1.1/media/upload.json

我已经尝试了这个不起作用:

if ($method == 'POST') {
    $oauth['status'] = $status;
    $oauth['media[]'] = ''; //trying with empty string
}

if ($method == 'POST') {
    $options[CURLOPT_POST] = true;
    $options[CURLOPT_POSTFIELDS] = 'status='.rawurlencode($status).'&media[]='.rawurlencode('');
}

0 个答案:

没有答案