推特 API ->用 php 更新个人资料 bg 图像
到目前为止,我一直在尝试使用 php 通过 twitter api 更新 twitter 个人资料 bg 图像......但没有成功
So far I have been trying to update the twitter profile bg image thr the twitter api with php... and without success
网上很多例子,包括这个:
通过 API 更新 Twitter 背景
还有这个
Twitter后台上传API和多表单数据
根本不起作用,大多数人在没有实际测试代码的情况下抛出答案.
Many examples on the web, including this one:
Updating Twitter background via API
and this one
Twitter background upload with API and multi form data
do not work at all, most ppl throw out answers without actually testing the code.
我发现直接将图片提交到twitter.com thr html表单,它会工作:
I found that directly submit the image to the twitter.com thr html form, it will work:
<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 帐户的用户名和密码)
(although the browser will prompt you for the twitter account username and password)
但是,如果我想使用 php 进行相同的过程,它会失败
However, if I want to go thr the same process with php, it fails
<?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>
这段代码的奇怪之处在于..它成功返回了 twitter XML,没有更新了个人资料背景图像.所以最后还是失败了.
The strange thing of this code is that.. it successfully returns the twitter XML, WITHOUT having the profile background image updated. So at the end it still fails.
非常感谢您阅读本文.如果您能提供帮助,那就太好了.请在抛出答案之前先测试您的代码,非常感谢.
Many thanks for reading this. It will be great if you can help. Please kindly test your code first before throwing out answers, many many thanks.
推荐答案
这对我有用(调试遗留的东西):
This is what works for me (debug stuff left in):
$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>';
相关文章