2016-11-22 648 views
1

为什么波波代码只发送一个图像而不是全部三个?如何使用twitter API发布多个图片?

我想要使用twitter API的多个图像单推文。在twitter API上提及您可以附加最多4张照片,1个动画GIF或1个视频。

$this->load->library('twitterext/tmhoauth'); 
$this->config->load('hybridauthlib', TRUE); 
$cunsumer = $this->config->item('hybridauthlib'); 
$tmhoauth = new tmhoauth(array(
    'consumer_key' => $cunsumer['providers']['Twitter']['keys']['key'], 
    'consumer_secret' => $cunsumer['providers']['Twitter']['keys']['secret'], 
    'user_token' => $np["networktoken"], 
    'user_secret' => $np["networksecret"], 
)); 

$media1='C:\wamp\www\vx\assets\uploads\post\post_10326.png'; 
$params1=array('media' => base64_encode(file_get_contents($media1)); 
$media2='C:\wamp\www\vx\assets\uploads\post\post_10327.png'; 
$params2=array('media' => base64_encode(file_get_contents($media2)); 
$media3='C:\wamp\www\vx\assets\uploads\post\post_10328.png'; 
$params3=array('media' =>base64_encode(file_get_contents($media3)); 

//after request 
$media_id=array(); 
for ($i=1; $i <4 ; $i++) { //suppose 

    $url = 'https://upload.twitter.com/1.1/media/upload.json'; 
    $code = $tmhoauth->request('POST', $url, $params.$i, true); 
    if ($code == 200) { 
     $response = json_decode($tmhoauth->response['response']); 
     $media_id[] = $response->media_id_string; 

    } else { 
     $response->error = $tmhoauth->response['response']; 
    } 


} 

$messageparams['media_ids']=$media_id; 
$messageparams['status']='my message'; 
$response = $adapter->api()->post('statuses/update.json', $messageparams); 

回答

2

你逝去的数组$ media_id

$messageparams['media_ids']=$media_id; 

但它应该是字符串数组没有的media_id,

$media_ids_str = implode(',', $media_id); 
$messageparams['media_ids']=$media_ids_str; 
+0

谢谢@iktedar – ML680

相关问题