Php Twitter发布推文与媒体

时间:2017-09-08 14:09:46

标签: php twitter

使用此代码,我可以上传图片并打印媒体ID,但我无法发布带有图片的推文。你能帮帮我吗?

由于

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_include_path('/home/.....');
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' => "..."
);
$twitter = new TwitterAPIExchange($settings);


$file = file_get_contents('aaa.jpg');
$data = base64_encode($file);

// Upload image to twitter
$url = "https://upload.twitter.com/1.1/media/upload.json";
$method = "POST";
$params = array(
"media_data" => $data,
);

$json = $twitter
->buildOauth($url, $method)
->setPostfields($params)
->performRequest();

// Result is a json string
$res = json_decode($json);
// Extract media id
$id = $res->media_id_string;
print_r($id);   

//////代码正在使用此行并可以打印媒体ID,如213213214123123

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

1 个答案:

答案 0 :(得分:0)

media_id值可用于使用statuses/update端点创建带有附加照片的推文。将您的media_id用作Statuses/update

中的参数