Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
推特API->;用php更新配置文件bg映像_Php_Upload_Twitter - Fatal编程技术网

推特API->;用php更新配置文件bg映像

推特API->;用php更新配置文件bg映像,php,upload,twitter,Php,Upload,Twitter,到目前为止,我一直在尝试用php更新twitter配置文件bg图像和twitter api。。。而且没有成功 网上有很多例子,包括这一个: 还有这个 根本不起作用,大多数ppl在没有实际测试代码的情况下抛出答案 我发现直接将图像提交到twitter.com thr html表单,它可以工作: <form action="http://twitter.com/account/update_profile_background_image.xml" enctype="multipart/f

到目前为止,我一直在尝试用php更新twitter配置文件bg图像和twitter api。。。而且没有成功

网上有很多例子,包括这一个:

还有这个


根本不起作用,大多数ppl在没有实际测试代码的情况下抛出答案

我发现直接将图像提交到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>

名称:
通过:
文件:
这段代码的奇怪之处在于。。它成功返回twitterxml,而不更新概要文件背景图像。所以最后还是失败了


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

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

我认为您使用的CURLOPT_POSTFIELDS方法是错误的。您需要根据PHP的curl命令,在文件的完整路径前面放置和@sign。您不应该输出整个文件内容

要在HTTP“post”操作中发布的完整数据。若要发布文件,请在文件名前加@并使用完整路径。这可以作为urlencoded字符串(如“para1=val1¶2=val2&…”)传递,也可以作为以字段名为键、字段数据为值的数组传递。如果值是数组,则内容类型标头将设置为多部分/表单数据

这是文档中的一个示例

$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>';


我希望这会有所帮助。

现在没有人能够真正更新个人资料背景图像或个人资料图像。 Twitter正在努力解决这个问题,直到没有解决方案为止。

这对我来说是可行的(调试内容留在:

$url='1!'http://twitter.com/account/update_profile_background_image.xml';
$uname='myuname';
$pword='mypword';
$img_path='/path/to/myimage.jpg';
$userpwd=$uname.:'$普沃德;
$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_数组($ch,$opts);
$response=curl\u exec($ch);
$err=旋度误差($ch);
$info=curl\u getinfo($ch);
卷曲关闭($ch);
回声';
回音$err。”
; 回显'-------------'
; 打印(信息); 回显'-------------'
; 回显htmlspecialchars($response)。'
; 回声';
“大多数ppl在没有实际测试代码的情况下给出答案”这里的大多数人给出的答案应该有助于向提问者指出正确的方向。由于您链接到的两个答案都被接受,这表明海报对答案指向的方向感到满意。为什么要手动构造HTTP auth头?尝试使用CURLOPT_HTTPAUTH和CURLOPT_USERPWD,如本例所示:有时,接受答案!=正确的方向。thx很多ben,我已经更新了代码,我已经尝试了几乎所有的方法让cURL上传,但无法让它工作。当然,即使在使用shell命令时也是如此。在我的问题中,我使用能够在twitter或html表单中更新的相同图像文件进行了测试,但是相同的图像文件在php版本中失败,并且您已经确认上载的图像正在成功地将其发送到
$target\u path
?如果是这样的话,听起来像是Twitter API的问题,我建议你向他们开一张罚单。嘿,但它可以与html表单一起使用。请阅读我的问题。当它与html表单一起工作而不是在php代码中工作时会很奇怪。请不要以为我是一个没有读过你的问题的白痴。twitterapi是出了名的脆弱,当web界面正常工作时,它常常无法工作。我使用了与您类似的代码来更新背景图像,这些代码有时有效,有时无效,所有这些都取决于API当天的感受。嘿,感谢您的回复,我不是说像“白痴”一样的东西,ppl快速阅读问题和答案是很常见的。如果你能分享你的代码那就太好了。。幸运的是,我以前也尝试过@version,但它根本不起作用。我尝试了很多方法向twitter提交文件,包括使用flash的UrlLolder和flash的filereference的upload等。你介意制作一个真正能够更新twitter背景的示例吗?非常感谢。不太感谢。事实上,您可以使用API thr纯html表单更新bg图像,如我的问题中所述。一个简单的html表单,直接提交到Twitter。问题在于使用API thr php
$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>';