推特 API ->用 php 更新个人资料 bg 图像 [英] Twitter API -> updating profile bg image with php

查看:12
本文介绍了推特 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("<", "&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,没有更新了个人资料背景图像.所以最后还是失败了.

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

这篇关于推特 API -&gt;用 php 更新个人资料 bg 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆