具有媒体ID /状态更新的Twitter API

时间:2015-12-22 16:48:04

标签: php twitter twitter-oauth

我有以下代码,除了一个问题,它将media_id附加到最终状态更新帖子之外。媒体ID似乎被忽略,文本状态仅上传。在媒体上传请求或状态帖上都没有输出错误代码。

有什么想法吗?

<?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
        )

)

2 个答案:

答案 0 :(得分:0)

尝试使用media_id_string而不是media_id

答案 1 :(得分:0)

我发现问题出在windows托管上,它不像php中的某些curl请求,因为它们不能一直工作。所以我将主机更改为Linux,之后它运行良好。