-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.php
27 lines (23 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
<?php
require "apikeys.php";
$key = $twitter_apikey;
$secret = $twitter_secret;
$api_endpoint = 'https://api.twitter.com/1.1/search/tweets.json?q=smartmirrorthesis&src=typd&result_type=recent&count=5'; // endpoint must support "Application-only authentication"
// request token
$basic_credentials = base64_encode($key.':'.$secret);
$tk = curl_init('https://api.twitter.com/oauth2/token');
curl_setopt($tk, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$basic_credentials, 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
curl_setopt($tk, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($tk, CURLOPT_RETURNTRANSFER, true);
$token = json_decode(curl_exec($tk));
curl_close($tk);
// use token
if (isset($token->token_type) && $token->token_type == 'bearer') {
$br = curl_init($api_endpoint);
curl_setopt($br, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token->access_token));
curl_setopt($br, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($br);
curl_close($br);
print($data);
}
?>