回答
那麼我得到的答案,下載Twitter Api爲php &創建了一個函數。
function image_upload(){
define('YOUR_CONSUMER_KEY' , 'your twitter app consumer key');
define('YOUR_CONSUMER_SECRET' , 'your twitter app consumer key secret');
require ('twitt/tmhOAuth.php');
require ('twitt/tmhUtilities.php');
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET",
'user_token' => "YOUR_OAUTH_TOKEN",
'user_secret' => "YOUR_OAUTH_TOKEN_SECRET",
));
$image = 'image.jpg';
$code = $tmhOAuth->request('POST','https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "@{$image};type=image/jpeg;filename={$image}",
'status' => 'message text written here',
),
true, // use auth
true // multipart
);
if ($code == 200){
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
}else{
tmhUtilities::pr($tmhOAuth->response['response']);
}
return tmhUtilities;
}
我只好一個'$ headers'變量添加到'$ tmhOAuth->請求()'調用。我從Twitter上找回了417錯誤,谷歌搜索引導我在multipart'true'之後插入'array('Expect'=>'')'。它在那之後完美運作。 – 2012-12-12 00:12:58
這仍然有效嗎? – 2013-01-10 07:45:48
我用於iPhone應用程序,這是在應用程序商店和是它仍然工作。 – 2013-01-10 07:47:29
那麼您的用戶必須使用您的APP獲得OAuth授權,然後才能使用API發佈推文。根據POST statuses/update & POST statuses/update_with_media, 但我有張貼圖片的麻煩(大約一年前,他們現在可能已經修復了)。
update_with_media已過時,應考慮使用以下方法:https://dev.twitter.com/rest/public/uploading-media
使用優秀hybridauth庫和更新Twitter.php setUserStatus功能有以下幾點,就可以達到你想要的東西:
/**
* update user status
* https://dev.twitter.com/rest/public/uploading-media-multiple-photos
*/
function setUserStatus($status)
{
if(is_array($status))
{
$message = $status["message"];
$image_path = $status["image_path"];
}
else
{
$message = $status;
$image_path = null;
}
$media_id = null;
# https://dev.twitter.com/rest/reference/get/help/configuration
$twitter_photo_size_limit = 3145728;
if($image_path!==null)
{
if(file_exists($image_path))
{
if(filesize($image_path) < $twitter_photo_size_limit)
{
# Backup base_url
$original_base_url = $this->api->api_base_url;
# Need to change base_url for uploading media
$this->api->api_base_url = "https://upload.twitter.com/1.1/";
# Call Twitter API media/upload.json
$parameters = array('media' => base64_encode(file_get_contents($image_path)));
$response = $this->api->post('media/upload.json', $parameters);
error_log("Twitter upload response : ".print_r($response, true));
# Restore base_url
$this->api->api_base_url = $original_base_url;
# Retrieve media_id from response
if(isset($response->media_id))
{
$media_id = $response->media_id;
error_log("Twitter media_id : ".$media_id);
}
}
else
{
error_log("Twitter does not accept files larger than ".$twitter_photo_size_limit.". Check ".$image_path);
}
}
else
{
error_log("Can't send file ".$image_path." to Twitter cause does not exist ... ");
}
}
if($media_id!==null)
{
$parameters = array('status' => $message, 'media_ids' => $media_id);
}
else
{
$parameters = array('status' => $message);
}
$response = $this->api->post('statuses/update.json', $parameters);
// check the last HTTP status code returned
if ($this->api->http_code != 200){
throw new Exception("Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus($this->api->http_code));
}
}
只是用它像這樣:
$config = "/path_to_hybridauth_config.php";
$hybridauth = new Hybrid_Auth($config);
$adapter = $hybridauth->authenticate("Twitter");
$twitter_status = array(
"message" => "Hi there! this is just a random update to test some stuff",
"image_path" => "/path_to_your_image.jpg"
);
$res = $adapter->setUserStatus($twitter_status);
或爲全文twitt:
$res = $adapter->setUserStatus("Just text");
只爲像我這樣的人絆倒在這一個,媒體功能已合併一年前;-) https://github.com/hybridauth/hybridauth/blob/master/hybridauth/Hybrid/Providers/Twitter.php#L191 – Can 2015-12-19 08:43:57
- 1. 在twitter上使用php共享圖像
- 2. 使用twitpic在twitter上上傳圖片
- 3. 需要在圖像上傳使用php
- 4. 使用JavaScript在Twitter上發佈圖像
- 5. 使用Ruby on Rails在twitter上上傳多張圖片Twitter Gem
- 6. 上傳圖像php
- 7. PHP上傳圖像
- 8. PHP圖像上傳
- 9. PHP圖像上傳
- 10. 使用PHP的圖像上傳Mysql
- 11. 無法使用php上傳圖像
- 12. 使用curl請求php圖像上傳
- 13. 使用php調整上傳的圖像
- 14. 使用PHP上傳安全圖像
- 15. 使用xcode上傳圖像(MySQL + PHP)
- 16. 將圖像上傳到GCS使用PHP
- 17. 上傳圖像到MySQL使用php
- 18. 使用php多圖像上傳
- 19. 無法使用php上傳圖像
- 20. uploadify不使用php上傳圖像
- 21. 使用PHP類上傳多個圖像
- 22. Twitter API使用PHP OAuth上傳
- 23. Unity3d將圖像上傳到Twitter
- 24. 使用twitter api在android上傳圖片至twitter
- 25. 無法使用roxyfileman在iis8上用php上傳圖像
- 26. Ruby-Twitter Gem - 使用INIT,APPEND和FINALIZE命令上傳圖像
- 27. 選擇多個圖像並使用PHP上傳圖像
- 28. PHP圖像上傳問題
- 29. PHP圖像上傳灰線
- 30. PHP圖像上傳麻煩
傢伙,如果這個問題有幫助,你也可以給予好評,不僅答案...... – 2013-03-14 07:26:12