简体   繁体   中英

Twitter Search API v1.1 with app only authentication

I have prepared my bearer token for app only auth as per the documentation and I have my header now, but I don't know how to send it to get a response:

      <?php
                $headers = array( 
                    "GET /1.1/search/tweets.json? HTTP/1.1", 
                    "Host: api.twitter.com", 
                    "User-Agent: my Twitter App v.1",

                    "Authorization: Bearer mybearertoken",


                ); 
$url = "https://api.twitter.com/1.1/search/tweets.json?q=%23SOMEHASHTAG&count=100";

Normally, with the v1, I would just do a $result=json_decode(file_get_contents($url), true) but I don't know how to proceed in this case.

You can play with the PHP's context parameters. More information can be found here: http://docs.php.net/context

And in your case the code would be something like the following:

$headers = array( 
            "GET /1.1/search/tweets.json? HTTP/1.1", 
             "Host: api.twitter.com", 
             "User-Agent: my Twitter App v.1",
             "Authorization: Bearer mybearertoken",
             ); 
$url = "https://api.twitter.com/1.1/search/tweets.json?q=%23SOMEHASHTAG&count=100";

$context = stream_context_create($headers);

$result=json_decode(file_get_contents($url, false, $context));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM