PHPoAuthLib icon indicating copy to clipboard operation
PHPoAuthLib copied to clipboard

how to upload media to twitter use PHPoAuthLib?

Open shushenghong opened this issue 10 years ago • 9 comments

Hi guys, this library is so awesome and just discard other php oauth libs.

i try to upload img to twitter:

$result = $service->request( "/statuses/update_with_media.json", "post", 
                    array (
                            'status' => $share->getMsg(),
                            'media[]' => file_get_contents( $share->getImg() ) 
                    ), array (
                            'Content-type' => 'Content-type: multipart/form-data' 
                    ) );

seems not working, can anyone help?

shushenghong avatar Apr 30 '14 17:04 shushenghong

Hey there,

Just wanted to let you know I've patched this in pull #297 to allow multipart transfers to work.

The syntax is a bit different now due to the way PHP's cURL library has changed, if you were to clone my changes then your request would look like this. I have fixed your content-type header too, the prefix is added further inside the library.

$result = $service->request( "/statuses/update_with_media.json", "POST", 
    array(
        'status' => $share->getMsg(),
        'media[]' => new CURLFile($share->getImg(), mime_content_type($share->getImg()), 'media[]')
    ), array(
        'Content-type' => 'multipart/form-data',
        // cURL will add this automatically when we do a file upload, and twitter will explode if it's present in the request headers.
        'Expect' => '',
    ) 
);

You might also have to force use of cURL, I found that my install was always choosing the stream method which is likely incapable of transferring binary data. I could be wrong though. I achieved this by creating a new OAuth\ServiceFactory and calling the setHttpClient() method on it when registering the OAuth service provider.

pospi avatar Aug 21 '14 05:08 pospi

Hi,

I'm having the same issue, I've set the Http Client to use curl, but getting the following response from Twitter

{"errors":[{"message":"Could not authenticate you","code":32}]}

My code looks like this

$result = $twitterService->request( "/statuses/update_with_media.json", "POST", array( 'status' => "hello", 'media[]' =>file_get_contents($image) ) , array( 'Content-Type' => 'multipart/form-data',

    'Expect' => '',
) 

);

Any ideas what the issue is? I've regenerated my API keys as I thought it was a permissions issue but still getting the same response.

jimwib avatar Jan 12 '15 20:01 jimwib

hey @jimwib, did you manage to make it work?

ndamnjanovic avatar Jan 14 '15 09:01 ndamnjanovic

No, still having the same problems

jimwib avatar Jan 14 '15 09:01 jimwib

@jimwib I think that URL you're trying to hit is wrong, at least according to Twitter API docs, it should be https://upload.twitter.com/1.1/media/upload.json. But I get Authorization Error, with it, as well.

Yes, I'm getting this Authorization error, no matter what URL try to hit: https://api.twitter.com/1.1/statuses/update_with_media.json, this one doesn't work either.

ndamnjanovic avatar Jan 14 '15 09:01 ndamnjanovic

I was wondering if it was something to do with the way the library assembles the request thats causing the issue

jimwib avatar Jan 14 '15 09:01 jimwib

yea, not sure, could be, I'll let you know, in case I find some solution.

ndamnjanovic avatar Jan 14 '15 09:01 ndamnjanovic

Hey dudes, can you reproduce an issue on the updated library?

logical-and avatar Apr 14 '15 18:04 logical-and

I'm still having this problem myself, I get a "Could not authenticate you." When trying to upload an image with my tweet. Regular Tweets work fine. Did anyone come up with a solution for this they could point me toward?

coogle avatar Nov 01 '15 21:11 coogle