Php 通过API更新Twitter背景

Php 通过API更新Twitter背景,php,xml,arrays,api,twitter,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,$targe

我在通过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_内容提取原始数据时,我得到了这个

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


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

好的,你不能将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";

您的原始代码实际上是正确的,除了说明“profile\u background\u image”=>“URL\u HERE”此处的URL\u需要是实际数据之外。因此,在发出OAUTH请求之前,您需要使用PHP cURL从图像URL中刮取图像数据,并将其放在profile\u background\u image选项中。您有使用PHP cURL的经验吗?如果不是的话,我当然可以为你指出正确的方向,但我担心会有一个轻微的学习曲线。我现在正在测试这个,看看实际通过了什么,让你知道。好吧,用更新的答案试试。基本上,你需要先将文件上传到服务器上,不管是通过表单上传还是直接拷贝,然后将其发布到Twitter上。嘿嘿,也许你在度假,但我真的希望你能解释整个魔术