简体   繁体   English

php Twitter与媒体发布推文

[英]Php Twitter Post tweet with media

With this code, I can upload image and print Media ID but I can't post a tweet with image. 使用此代码,我可以上传图像并打印媒体ID,但是不能发布带有图像的推文。 Could you help me? 你可以帮帮我吗?

Thanks 谢谢

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

////// Code is working to this line and can print media id like 213213214123123 //////代码在此行中有效,并且可以打印媒体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();

The media_id value can be used to create a Tweet with an attached photo using the statuses/update endpoint. media_id值可用于使用statuses/update终结点创建带有附带照片的推文。 Use your media_id as a parameter in the Statuses/update 使用您的media_id作为状态/更新中的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM