通过API更新Twitter背景

时间:2009-09-27 09:07:39

标签: php xml arrays api twitter

我在使用Twitter的API更新背景时遇到了一些麻烦。

$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');

当我尝试通过cURL或file_get_contents提取原始数据时,我得到了这个......

  
    

期望失败期望请求标头中给出的期望     该服务器无法满足该字段。     客户发送         期望:100-继续,但我们只允许100-继续期望。

  

2 个答案:

答案 0 :(得分:1)

好吧,鉴于错误消息,听起来您应该自己加载URL的内容,并直接发布数据。你试过了吗?

答案 1 :(得分: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";