我是使用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));
有谁能帮帮我吗?
您没有遵循上传媒体的TwitterOAuth文档。
$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);
我找到了我的问题的解决方案,它帮助我解决了上传图片的问题。
$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;
}