-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitter.php
33 lines (26 loc) · 1.02 KB
/
twitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
header("Access-Control-Allow-Origin: *");
// convert key/secret to credentials
$credentials = base64_encode(rawurlencode($key).":".rawurlencode($secret));
// use credentials to get token
$authContext = stream_context_create(array(
"http" => array(
"method" => "POST",
"header" => "Authorization: Basic ".$credentials."\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
"content" => "grant_type=client_credentials"
)
));
$authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);
$decodedAuth = json_decode($authResponse, true);
$bearerToken = $decodedAuth["access_token"];
// use token to get data
$context = stream_context_create(array(
"http" => array(
"method" => "GET",
"header" => "Authorization: Bearer " . $bearerToken . "\r\n"
)
));
$encodedData = file_get_contents("https://api.twitter.com/1.1/search/tweets.json?".$_SERVER['QUERY_STRING'], false, $context);
echo $encodedData;
?>