开发者

Twitter API -> updating profile bg image with php

开发者 https://www.devze.com 2022-12-15 02:23 出处:网络
So far I have been trying to update the twitter profile bg image thr the twitter api with php... and without success

So far I have been trying to update the twitter profile bg image thr the twitter api with php... and without success

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.

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>

(although the browser will prompt you for the twitter account username and password)

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>

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


Have you confirmed that the image you expect is present and being sent (i.e. echo out the base64 encoded data for verification)? Is it GIF/PNG/JPG and under the 800 kilobyte limit set by the API?


I think you're using the CURLOPT_POSTFIELDS method wrong. You need to put and @ sign in front of the full path to the file according to the documentation for curl PHP. You are not supposed to output the whole file contents.

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

This is an example from the documentation.

<?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);
?>

I hope this helps.


Right now no one can actualy update the profile background image nor the profile image. Twitter is working to fix that issue till then there is no fix.

0

精彩评论

暂无评论...
验证码 换一张
取 消