2009-12-28 125 views
2

到目前为止,我一直在试图更新THR用PHP Twitter的API的Twitter主页背景图片...并没有成功Twitter的API - >用PHP更新个人资料背景图片

许多例子在网络上,包括本之一:
Updating Twitter background via API
这一个
Twitter background upload with API and multi form data
不会在所有的工作,最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帐户的用户名和密码)

但是,如果我想去THR与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> 

这段代码的奇怪的是..它成功返回的XML叽叽喳喳,没有已更新配置文件背景图像。所以最后它仍然失败。

非常感谢您阅读本文。如果你能提供帮助,情况会很好。请在扔出答案之前先测试你的代码,非常感谢。

+6

“最PPL抛出了答案,而无需实际测试代码”这里的大多数人扔出去的答案,应该帮助一点问题提问者在右方向。因为你连接的两个答案都被接受了,这表明海报人员对答案指向他们的方向感到满意。 – ceejayoz 2009-12-28 03:32:29

+1

为什么手动构建的HTTP auth头文件?尝试使用CURLOPT_HTTPAUTH和CURLOPT_USERPWD,如下例所示:http://www.higherpass.com/php/Tutorials/Using-Curl-To-Query-Remote-Servers/5/ – 2009-12-28 03:47:55

+2

有时候,接受的答案!=正确的方向。 – Unreality 2009-12-28 03:57:49

回答

5

这对我来说是什么在起作用(调试东西留在):

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

非常感谢您的全部来源! – Unreality 2010-01-06 08:42:58

0

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

+0

。我测试了使用相同的图像文件,可以更新里面的twitter或在我的问题的HTML表单,但相同的图像文件失败的PHP版本 – Unreality 2009-12-28 03:55:01

+0

而且你已经证实上传的图像正在成功''target_path '?如果是这样,听起来像Twitter API的问题,我建议与他们打开一张票。 – ceejayoz 2009-12-28 04:06:39

+0

嘿,但它适用于html表单。请阅读我的问题。当它与html表单一起使用时,它会很奇怪,但在php代码中却不会。 – Unreality 2009-12-28 04:08:02

0

我想你使用CURLOPT_POSTFIELDS方法错了。您需要根据卷曲PHP的documentation,在文件的完整路径前放置@并签名。你不应该输出整个文件内容。

要在HTTP“POST”操作中发布的完整数据。要发布文件,请使用@预先指定文件名并使用完整路径。这可以作为urlencoded字符串传递,如'para1 = val1 & para2 = val2 & ...'或作为字段名称作为键和字段数据作为值的数组。如果value是一个数组,则Content-Type头将被设置为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); 
?> 

我希望这有助于。

+0

不幸的是,我也尝试过@版本,它根本不起作用。我已经尝试了许多方式向twitter提交文件,包括使用flash的URLLorder和flash的filereference的上传等。你是否介意制作一个真正能够更新twitter背景的样本?非常感谢。 – Unreality 2010-01-01 11:34:36

0

现在没有人可以实际更新配置文件背景图像和配置文件图像。 Twitter正在努力解决这个问题,直到那时没有修复。

+0

不是。实际上,您可以使用我的问题中提到的API thr纯文本格式来更新bg图像。直接提交给Twitter的这种http://code.dchammer.com/twitter/test3.php这种简单的html表单。问题在于使用API​​ thr php – Unreality 2010-01-04 07:01:41

相关问题