使用亚伯拉罕·威廉姆斯sdk在php中的Twitter 1.1图像上传问题

时间:2015-07-17 09:41:51

标签: php twitter

我是使用php sdk在Twitter帐户中发布图片的新手。

我已经尝试但尚未得到解决方案。

我的代码是

$connection = new TwitterOAuth($twitter_key, $twitter_secret, $token, $secret);

$fileContent = file_get_contents($imageUrl);
$base64   = base64_encode($fileContent);
$fileContent = ('data:' . $mime . ';base64,' . $base64);

 $connection->post("https://upload.twitter.com/1.1/media/upload.json", array("media_data" =>$fileContent));

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

您没有关注TwitterOAuth documentation上传媒体。

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
$media1 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten1.jpg']);
$media2 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten2.jpg']);
$parameters = [
    'status' => 'Meow Meow Meow',
    'media_ids' => implode(',', array($media1->media_id_string, $media2->media_id_string)),
];
$result = $connection->post('statuses/update', $parameters);

答案 1 :(得分:1)

我找到了问题解决方案,帮助我解决上传图片。

$url = 'https://upload.twitter.com/1.1/media/upload.json';
$method = 'POST';
$parameters = array('media' => base64_encode(file_get_contents($filePath)), );
$request = OAuthRequest::from_consumer_and_token($connection->consumer, $connection->
token, $method, $url, $parameters);
$request->sign_request($connection->sha1_method, $connection->consumer, $connection->
token);

$response = $connection->http($request->get_normalized_http_url(), $method, $request->
to_postdata());
if ($connection->format === 'json' && $connection->decode_json)
{
    $response = json_decode($response);
    $mediaId[] = $response->media_id_string;
}