通过API更新Twitter背景
问题描述:
我通过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-继续期望。
答
好的,你不能将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";
答
好,给出的错误信息,这听起来像你应该自己加载URL的内容,并直接上传数据。你尝试过吗?
您的原始代码实际上是正确的,期待它指出URL_HERE需要成为实际数据的'profile_background_image'=>'URL_HERE'。因此,甚至在发出OAUTH请求之前,您需要使用PHP cURL从图像URL中刮取图像数据,并将其放置在profile_background_image选项中。 – jakeisonline 2009-09-27 09:36:32
你有没有使用PHP cURL的经验?如果不是这样,我当然可以指出你的方向是正确的,但是会有轻微的学习曲线。我很害怕 – jakeisonline 2009-09-27 10:15:10
我现在正在测试这个,会看看实际正在传递的内容,并让你知道。 – jakeisonline 2009-09-27 10:51:02