【问题标题】:Updating Twitter background via API通过 API 更新 Twitter 背景
【发布时间】:2023-03-30 07:30:01
【问题描述】:

我在通过 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 请求头中给出的期望 此服务器无法满足字段。 客户端发送 Expect: 100-continue 但我们只允许 100-continue 的期望。

【问题讨论】:

    标签: php xml arrays api twitter


    【解决方案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";
    

    【讨论】:

    • 您的原始代码实际上是正确的,期望它指出'profile_background_image' => 'URL_HERE' URL_HERE 需要是实际数据。因此,在发出 OAUTH 请求之前,您需要使用 PHP cURL 从图像 URL 中抓取图像数据并将其放在 profile_background_image 选项中。
    • 您对 PHP cURL 有任何经验吗?如果没有,我当然可以为您指明正确的方向,但恐怕会有一点学习曲线
    • 我现在正在对此进行测试,将看看实际传递的内容并通知您。
    • 好的,用更新的答案试试。从本质上讲,您需要先将文件上传到您的服务器,无论是通过表单上传还是直接复制,然后将其发布到 Twitter。
    • 嘿嘿也许你在度假,但我真的希望你能解释整个魔术技巧
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 2011-09-29
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      相关资源
      最近更新 更多