到目前为止,我一直在试图更新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("<", "<", $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叽叽喳喳,没有已更新配置文件背景图像。所以最后它仍然失败。
非常感谢您阅读本文。如果你能提供帮助,情况会很好。请在扔出答案之前先测试你的代码,非常感谢。
“最PPL抛出了答案,而无需实际测试代码”这里的大多数人扔出去的答案,应该帮助一点问题提问者在右方向。因为你连接的两个答案都被接受了,这表明海报人员对答案指向他们的方向感到满意。 – ceejayoz 2009-12-28 03:32:29
为什么手动构建的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
有时候,接受的答案!=正确的方向。 – Unreality 2009-12-28 03:57:49