问题描述
我在通过 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 提取原始数据时,我得到了这个...
Expectation Failed 在 Expect 请求头中给出的期望此服务器无法满足字段.客户端发送期望:100-continue 但我们只允许 100-continue 期望.
好吧,你不能将 Twitter 定向到一个 URL,它不会接受.环顾四周,我发现最好的方法是将图像下载到本地服务器,然后像上传表单一样将其传递到 Twitter.
试试下面的代码,让我知道你得到了什么.
//我们想要抓取的来自外部(或内部)服务器的 URL$url = 'http://www.google.com/logos/11th_birthday.gif';//我们需要抓取这个的文件名,除非你想自己创建$filename = basename($url);//这是我们将新文件保存到的位置.将 LOCALPATH 替换为您要将文件保存到的路径,即 www/home/content/my_directory/$newfilename = 'LOCALPATH' .$文件名;//复制过来,PHP 会处理开销.复制($url,$newfilename);//现在是 OAuth 时间了...手指交叉!$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $newfilename), 'POST');//回显一些东西,这样你就知道它通过了打印完成";
I'm having a little trouble updating backgrounds via Twitter's 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');
When I try to pull the raw data via cURL or file_get_contents, I get this...
OK, you can't direct Twitter to a URL, it won't accept that. Looking around a bit I've found that the best way is to download the image to the local server and then pass that over to Twitter almost like a form upload.
Try the following code, and let me know what you get.
// 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";
这篇关于通过 API 更新 Twitter 背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!