2009-09-27 84 views
0

我通過Twitter的API更新背景有點麻煩。通過API更新Twitter背景

$target_url = "http://www.google.com/logos/11th_birthday.gif"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
$html = curl_exec($ch); 

$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $html), 'POST'); 

當我嘗試通過捲曲或的file_get_contents拉的原始數據,我得到這個...

預期失敗的預期要求頭 場給出的期望不能通過這臺服務器遇見。 客戶發送 期望:100-繼續,但我們只允許100-繼續期望。

回答

1

好的,你不能將Twitter引導到一個URL,它不會接受。回顧一下,我發現最好的方法是將圖像下載到本地服務器,然後將它傳遞給Twitter,就像上傳表單一樣。

請嘗試下面的代碼,並讓我知道你得到了什麼。

// The URL from an external (or internal) server we want to grab 
$url = 'http://www.google.com/logos/11th_birthday.gif'; 

// We need to grab the file name of this, unless you want to create your own 
$filename = basename($url); 

// This is where we'll be saving our new file to. Replace LOCALPATH with the path you would like to save the file to, i.e. www/home/content/my_directory/ 
$newfilename = 'LOCALPATH' . $filename; 

// Copy it over, PHP will handle the overheads. 
copy($url, $newfilename); 

// Now it's OAuth time... fingers crossed! 
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $newfilename), 'POST'); 

// Echo something so you know it went through 
print "done"; 
+2

您的原始代碼實際上是正確的,期待它指出URL_HERE需要成爲實際數據的'profile_background_image'=>'URL_HERE'。因此,甚至在發出OAUTH請求之前,您需要使用PHP cURL從圖像URL中刮取圖像數據,並將其放置在profile_background_image選項中。 – jakeisonline 2009-09-27 09:36:32

+0

你有沒有使用PHP cURL的經驗?如果不是這樣,我當然可以指出你的方向是正確的,但是會有輕微的學習曲線。我很害怕 – jakeisonline 2009-09-27 10:15:10

+0

我現在正在測試這個,會看看實際正在傳遞的內容,並讓你知道。 – jakeisonline 2009-09-27 10:51:02

1

好,給出的錯誤信息,這聽起來像你應該自己加載URL的內容,並直接上傳數據。你嘗試過嗎?