Posting by XMLHttpRequest a canvas.todataURL to twitter's account/update_profile_image with tmhOAuth


#1

Hello everyone :slight_smile:

I’m trying to upload a canvas.toDataURL() to twitter account/update_profile_image with this codes:

[ javascript ]

    var xhr = new XMLHttpRequest(),
        fileUpload = xhr.upload,
        boundary = 'multipartformboundary' + (new Date).getTime();
    fileUpload.addEventListener("load", function(ajax){
        console.debug(ajax); // getting the response
    }, false);

    xhr.open("POST", "uploadimage.php", true);
    xhr.setRequestHeader('content-type', 'multipart/form-data; boundary='+ boundary);

    builder = '--' + boundary + '\r\n Content-Disposition: form-data; name="image"; filename="upload.png"\r\n Content-Type: image/png \r\n\r\n'; 
    builder += (canvas.toDataURL('image/png').split(","))[1];

    builder += '\r\n--' + boundary + '--\r\n';
    xhr.send(builder);

[ uploadimage.php ]

    var_dump($_FILES);

    if ( isset($_SESSION['access_token']) ) {
        $tmhOAuth->config['user_token']  = $_SESSION['access_token']['oauth_token'];
        $tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];

        $code = $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
        if ($code == 200) {
            $R = json_decode($tmhOAuth->response['response']);

            $params = array(
                'image' => "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
            );

            $code = $tmhOAuth->request('POST', $tmhOAuth->url("1/account/update_profile_image"),
                $params,
                true, // use auth
                true // multipart
            );

            if ($code == 200) {
                tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
            }
            tmhUtilities::pr(htmlentities($tmhOAuth->response['response']));

        } else {
            outputError($tmhOAuth);
        }
    }

So the var_dump($_FILES) throw this:

    array(1) {
      ["image"]=>
      array(5) {
        ["name"]=>
        string(11) "upload.png"
        ["type"]=>
        string(9) "image/png"
        ["tmp_name"]=>
        string(14) "/tmp/phpMe8k3c"
        ["error"]=>
        int(0)
        ["size"]=>
        int(2432)
      }
    }

And I get this twitter response:

    <pre style="word-wrap: break-word">{&quot;error&quot;:&quot;There was a problem with your picture. Probably too big.&quot;,&quot;request&quot;:&quot;\/1\/account\/update_profile_image.json&quot;}</pre>

What do you think is happening? I’m stuck in this…

My resources to the above codes are:


#2

Dudes, I solved this, please remove the question.


#3

my date in my twitter account is late of 15 hours here in place…if i post or received tweets it always has yesterday not the minutes i tweeted


#4

How did you solve it?


#5

I could solve it by using only using base64_encoded string. No need to use any other meta information.


#6

#7

#8

#9