2017-03-29 25 views
1

私はPHPを使用してTwitterにメディアを投稿しようとしています。私はツイートを投稿できますが、画像は見られません。私たちを手伝ってくれますか?PHP Twitterの投稿画像

<?php 
require_once('TwitterAPIExchange.php'); 
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/ 
$settings = array(
    'oauth_access_token' => "", 
    'oauth_access_token_secret' => "", 
    'consumer_key' => "", 
    'consumer_secret' => "" 
); 

$media_id = '847072976486858753'; 
$url = "https://api.twitter.com/1.1/statuses/update.json"; 
$twitter = new TwitterAPIExchange($settings); 
$requestMethod = 'POST'; 
$response = $twitter->setPostfields(
    array('status' => 'Test Tweet', 'media_ids' => $media_id) 
)->buildOauth($url, $requestMethod) 
    ->performRequest(); 
?> 

答えて

0

イメージ処理部分がありません。

$url_media = "https://api.twitter.com/1.1/statuses/update_with_media.json"; 
$requestMethod = "POST"; 

$tweetmsg = $_POST['post_description']; 
$twimg = $_FILES['pictureFile']['tmp_name']; 

    $postfields = array(
     'status' => $tweetmsg, 
     'media[]' => '@' . $twimg 
    ); 
    try { 
     $twitter = new TwitterAPIExchange($settings); 
     $twitter->buildOauth($url_media, $requestMethod) 
       ->setPostfields($postfields) 
       ->performRequest(); 

     echo "You just tweeted with an image"; 
    } catch (Exception $ex) { 
     echo $ex->getMessage(); 
    } 
+1

update_with_mediaエンドポイントは償却されます。 –

1

あなたのコードはうまく見えますが、あなたが使用している$ media_idは有効だと思いますか?ここでは、最初に画像のアップロードを処理し、つぶやきを投稿するコードがあります。

// send image to Twitter first 
$url = 'https://upload.twitter.com/1.1/media/upload.json'; 
$requestMethod = 'POST'; 

$image = 'full/path/to/image.jpg'; 

$postfields = array(
    'media' => base64_encode(file_get_contents($image)) 
); 

$response = $twitter->buildOauth($url, $requestMethod) 
    ->setPostfields($postfields) 
    ->performRequest(); 

// get the media_id from the API return 
$media_id = json_decode($response)->media_id; 

// then send the Tweet along with the media ID 
$url = 'https://api.twitter.com/1.1/statuses/update.json'; 
$requestMethod = 'POST'; 

$postfields = array(
    'status' => 'My amazing tweet' 
    'media_ids' => $media_id, 
); 

$response = $twitter->buildOauth($url, $requestMethod) 
    ->setPostfields($postfields) 
    ->performRequest();