Problems with Twitter API 1.1 - application-only authentication response


#1

I’m trying to retrieve data from Twitter by connecting to its API and make some requests but I always get nothing in return… I just requested the bearer token and successfully received it.

Here is the code in PHP:

$url = “https://api.twitter.com/1.1/statuses/user_timeline.json?count=10&screen_name=twitterapi”;
$headers = array(
“GET”.$url." HTTP/1.1",
“Host: api.twitter.com”,
“User-Agent: My Twitter App v1.0.23”,
“Authorization: Bearer “.$bearer_token.””,
“Content-Type: application/x-www-form-urlencoded;charset=UTF-8”,
);

$ch = curl_init(); // setup a curl
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
$retrievedhtml = curl_exec ($ch); // execute the curl

print_r($retrievedhtml);
var_dump($retrievedhtml);


Here is another way I have tried but also I got the same results

    $url = "https://api.twitter.com/1.1/statuses/user_timeline.json?count=10&screen_name=twitterapi";

$context = stream_context_create(array("https" => array("method" => "GET",
        "header" => "GET /1.1/statuses/user_timeline.json?count=100&screen_name=twitterapi HTTP/1.1 \r\n" .
        "Host: api.twitter.com \r\n" .
        "User-Agent: My Twitter App v1.0.23 \r\n" .
        "Authorization: Bearer " . $bearer_token . "\r\n" .
        "Accept-Encoding: gzip \r\n"  )));

     $data = @file_get_contents($url, true, $context);
     $respone= json_decode($data, true);
     
      print_r($respone);
      var_dump($respone);

when using the print_r nothing is shown at all and when using the var_dump i found “bool(false)”

Any idea with what could be wrong with this?

Regards,


#2

Ashraf —

Were you able to successfully make requests to this API endpoint using twurl (https://github.com/marcel/twurl)? That’s usually a good way to diagnose the issue; if you are able to make calls using Twurl, then there is likely something amiss with your code. You use the headers and responses from Twurl to debug your code.

Cheers,

Vince