Skip to content
Advertisement

PHP Fetch image from DM Twitter using REST API?

I have problem when I want to fetch image that was sent to my DM. So when I fetch the DM data from API, I got the media id for the image but how to fetch it?

I want to fetch the image and tweet it to my account.

So basically:

  • Someone send DM to me with media
  • I read my account DM from API
  • Tweet DM content to my account

Here is my code

    $AMConnection = new TwitterOAuth(AM_CONSUMER_KEY, AM_CONSUMER_SECRET);
    $AMConnection->setAccessToken($amService->token, $amService->secret);
    $jsonData = json_decode(file_get_contents("./fetchDM.json"));
    foreach ($jsonData as $json) {
        if (!isset($json->entities->media)) continue;
        $medias = $json->entities->media;
        $postParam['status'] = $json->text;
        foreach ($medias as $image) {
            $postParam['status'] = str_replace($image->url, "", $postParam['status']);
            $postParam['media_ids'][] = $image->id_str;
        }

        echo "SEND TWEET".PHP_EOL;
        $return = $AMConnection->post('statuses/update', $postParam);
        var_dump($return);
        if (isset($return->errors) and count($return->errors) > 0) {
            foreach ($return->errors as $error) {
                if ($error->code == 89) {
                    $amService->token = null;
                    $amService->secret = null;
                    $amService->save();
                    break;
                }
            }
        }
    }

On my code, I was tried to tweet media using media_id but it doesn’t work. Sorry for bad english

Advertisement

Answer

Finally it’s solved.
So I’ve been look for this one on some forums but only 2 forum has answer, 1 on twitter forum (sorry I don’t have link) and another 1 StackOverflow

I’m using library and try messing out with it but deadend. The library that I’ve used to request REST API, use OAuth request from URL and POST data. Yeah, it’s totally work (only for REST API) but this one is totally different.

To fetch image from DM, YOU MUST REQUEST USING HEADER AUTHORIZATION and no other way.

On my sample, I want to try to fetch https://ton.twitter.com/1.1/ton/data/dm/863758539021365258/863758532109246464/VSSysEvy.jpg (This one is image url from DM, so only me and someone who’ve sent me that image only can see it).

I’m using CURL PHP to fetch the image so the request will be like this

<?php
$this->http_info = array();
$ci = curl_init();
$url = 'https://ton.twitter.com/1.1/ton/data/dm/863758539021365258/863758532109246464/VSSysEvy.jpg';
$headerAuth = 'Authorization: OAuth oauth_version="1.0",
                oauth_nonce="{autoGeneratedNonce}",
                oauth_timestamp="{time()}",
                oauth_consumer_key="{yourAppToken}",
                oauth_token="{userToken}",
                oauth_signature_method="HMAC-SHA1",
                oauth_signature="{autoGeneratedSignature}"';
/* Curl settings */
curl_setopt($ci, CURLOPT_USERAGENT, 'LShaf User Agent');
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_HTTPHEADER, [$headerAuth]);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_HEADER, FALSE);
curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);
curl_close ($ci);

Note: You MUST use method GET and put Authorization on HEADER.
And for the last thing, I’m sorry for my bad english.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement