dououde4065 2015-12-22 16:48
浏览 49
已采纳

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

I have the following code, which works well apart from one issue, which is attaching the media_id to the final status update post. The media id just seems to be ignored and the text status is uploaded only. There is no error codes being output on either media upload request or status post.

Any ideas?

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

The media upload output looks complete

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条回答 默认 最新

  • douren6874 2017-01-24 15:21
    关注

    I found the problem was with the windows hosting, it doesn't like certain curl requests in php as they don't always work. So I changed the hosting to Linux and it worked fine after that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程