views:

314

answers:

1

Taken from the Twitter's API section.

Why do my image uploads always fail? The image update methods require multipart form data. They do not accept a URL to an image not do they accept the raw image bytes. They instead require the data to be delivered in the form of a file upload.

Has anyone came to a conclusion with this, or resolved this issue? I'm having various amounts of trouble trying to get it to post an image. I've looked around and found no solutions with this.

A: 

What exact problems are you having? From the sounds of the API, you just need to do a regular file upload to Twitter itself. The following allows you to upload a file to your server and push it to Twitter via the API docs:

<?php
    if( $_POST ) {
        // Do anything needed for authentication
        $ch = curl_init('http://twitter.com/account/update_profile_background_image.xml');
        curl_setopt_array(array(
            CURLOPT_POSTFIELDS => array('image' => '@'.$_FILES['myfile']['tmp_name']),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
        ));

        $rsp = curl_exec($ch);
        // Read the response
    }
?>
<form enctype="multipart/form-data" method="post">
    File: <input type="file" name="myfile" />
    <input type="submit">
</form>

More information can be found in the PHP documentation and the cURL documentation for PHP.

dragonmantank
How would I add this into what you wrote? $content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $html), 'POST');
Homework
obviously the one who answered this question hasn't tested the code at all. For example, curl_setopt_array requires to be curl_setopt_array($ch, array(... instead of curl_setopt_array(array( . All in all the answer failed.
Unreality
The code may not work as-written, but it's entirely possible it gave the original poster a clue as to the right direction.
ceejayoz
tested and failed even with those errors fixed
Unreality
A workable solution can be found here: http://stackoverflow.com/questions/1967831/twitter-api-updating-profile-bg-image-with-php
Unreality