【问题标题】:Twitter API -> updating profile bg image with phpTwitter API -> 使用 php 更新个人资料 bg 图像
【发布时间】:2026-01-27 04:55:01
【问题描述】:

到目前为止,我一直在尝试使用 php 通过 twitter api 更新 twitter 个人资料 bg 图像......但没有成功

网上有很多例子,包括这个:
Updating Twitter background via API
还有这个
Twitter background upload with API and multi form data
根本不起作用,大多数人在没有实际测试代码的情况下抛出答案。

我发现直接将图片提交到twitter.com thr html表单,它会工作:

<form action="http://twitter.com/account/update_profile_background_image.xml" enctype="multipart/form-data" method="post">
    File: <input type="file" name="image" /><br/>
    <input type="submit" value="upload bg">
</form>

(尽管浏览器会提示您输入 twitter 帐户的用户名和密码)

但是,如果我想使用 php 进行相同的过程,它会失败

<?php
if( isset($_POST["submit"]) ) {

    $target_path = "";
    $target_path = $target_path . basename( $_FILES['myfile']['name']); 

    if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
        // "The file ".  basename( $_FILES['myfile']['name']). " has been uploaded<br/>";
    } else{
        // "There was an error uploading the file, please try again!<br/>";
    }

    $ch = curl_init('http://twitter.com/account/update_profile_background_image.xml');
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, $_POST['name'] . ':' . $_POST['pass']);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode(file_get_contents($target_path))));

    $rsp = curl_exec($ch);
    echo "<pre>" . str_replace("<", "&lt;", $rsp) . "</pre>";

}
?>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="submit" value="1"/>
name:<input type="text" name="name" value=""/><br/>
pass:<input type="password" name="pass" value=""/><br/>
File: <input type="file" name="myfile" /><br/>
<input type="submit" value="upload bg">
</form>

这段代码的奇怪之处在于..它成功返回了 twitter XML,没有更新了个人资料背景图像。所以最后还是失败了。

非常感谢您阅读本文。如果您能提供帮助,那就太好了。请在抛出答案之前先测试您的代码,非常感谢。

【问题讨论】:

  • “大多数人在没有实际测试代码的情况下抛出答案”这里的大多数人抛出的答案应该有助于将提问者指向正确的方向。由于您链接到的两个答案都被接受,这表明发布者对答案指向的方向感到满意。
  • 为什么要手动构建 HTTP auth 标头?尝试使用 CURLOPT_HTTPAUTH 和 CURLOPT_USERPWD,如下例所示:higherpass.com/php/Tutorials/Using-Curl-To-Query-Remote-Servers/…
  • 很多时候,接受的答案!=正确的方向。
  • 谢谢很多本,我已经更新了代码
  • 我已经尝试了几乎所有的 cURL 来让它上传,但无法让它工作。即使使用 shell 命令。

标签: php upload twitter


【解决方案1】:

这对我有用(调试的东西留在里面):

$url      = 'http://twitter.com/account/update_profile_background_image.xml';
$uname    = 'myuname';
$pword    = 'mypword';
$img_path = '/path/to/myimage.jpg';
$userpwd  = $uname . ':' . $pword;
$img_post = array('image' => '@' . $img_path . ';type=image/jpeg',
                  'tile'  => 'true');

$opts = array(CURLOPT_URL => $url,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_HEADER => true,
              CURLOPT_POST => true,
              CURLOPT_POSTFIELDS => $img_post,
              CURLOPT_HTTPAUTH => CURLAUTH_ANY,
              CURLOPT_USERPWD => $userpwd,
              CURLOPT_HTTPHEADER => array('Expect:'),
              CURLINFO_HEADER_OUT => true);

$ch = curl_init();
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
$err      = curl_error($ch);
$info     = curl_getinfo($ch);
curl_close($ch);

echo '<pre>';
echo $err . '<br />';
echo '----------------' . '<br />';
print_r($info);
echo '----------------' . '<br />';
echo htmlspecialchars($response) . '<br />';
echo '</pre>';

【讨论】:

    【解决方案2】:

    您是否确认您期望的图像存在并且正在发送(即回显 base64 编码数据以进行验证)?是否为 GIF/PNG/JPG 且低于 API 设置的 800 KB 限制?

    【讨论】:

    • 当然。我使用能够在 twitter 内更新的相同图像文件或我的问题中的 html 表单进行了测试,但 php 版本的相同图像文件失败
    • 您已经确认上传的图片成功发送到$target_path?如果是这样,听起来像是 Twitter API 的问题,我建议向他们开一张票。
    • 嘿,但它适用于 html 表单。请阅读我的问题。使用html表单而不是php代码时会很奇怪。
    • 请停止假设我是一个没有阅读您问题的白痴。 Twitter API 是出了名的不稳定,在 Web 界面正常工作时经常无法工作。我使用了与您类似的代码来更新背景图像,该代码有时会起作用,有时会不起作用,这一切都取决于 API 当天的感受。
    • 嘿,感谢您的回复,我确实不是的意思是“白痴”之类的东西,人们快速阅读问题和答案是很常见的。如果您能分享您的代码,那就太好了。非常感谢
    【解决方案3】:

    我认为您使用错误的 CURLOPT_POSTFIELDS 方法。根据 curl PHP 的documentation,您需要在文件的完整路径前面加上和@ 符号。您不应该输出整个文件内容。

    要在 HTTP“POST”操作中发布的完整数据。要发布文件,请在文件名前加上 @ 并使用完整路径。这可以作为 urlencoded 字符串传递,例如 'para1=val1&para2=val2&...' 或作为字段名称作为键和字段数据作为值的数组。如果 value 是一个数组,Content-Type header 将被设置为 multipart/form-data。

    这是文档中的一个示例。

    <?php
    /* http://localhost/upload.php:
    print_r($_POST);
    print_r($_FILES);
    */
    
    $ch = curl_init();
    
    $data = array('name' => 'Foo', 'file' => '@/home/user/test.png');
    
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    curl_exec($ch);
    ?>
    

    我希望这会有所帮助。

    【讨论】:

    • 不幸的是我之前也尝试过@版本,它根本不起作用。我尝试了很多方法来向twitter提交文件,包括使用flash的URLLorder和flash的filereference的上传等。你介意制作一个真正能够更新twitter背景的真实样本吗?非常感谢。
    【解决方案4】:

    目前没有人可以实际更新个人资料背景图片或个人资料图片。 Twitter 正在努力解决这个问题,直到那时还没有解决办法。

    【讨论】:

    • 并非如此。实际上,您可以使用我的问题中提到的 API thr 纯 html 表单来更新 bg 图像。像 code.dchammer.com/twitter/test3.php 这样的纯 html 表单,直接提交到 Twitter。问题在于使用 API thr php
    最近更新 更多