Skip to content

Instantly share code, notes, and snippets.

@arlg
Created November 28, 2016 21:39
Show Gist options
  • Save arlg/76ab1a59af0110fda0bef5c7a3e26490 to your computer and use it in GitHub Desktop.
Save arlg/76ab1a59af0110fda0bef5c7a3e26490 to your computer and use it in GitHub Desktop.
Tweet search
// auth parameters
$api_key = urlencode(''); // Consumer Key (API Key)
$api_secret = urlencode(''); // Consumer Secret (API Secret)
$auth_url = 'https://api.twitter.com/oauth2/token';
// what we want?
$data_username = ''; // username
$data_count = 1; // number of tweets
$data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; // Change if you need to search for something else
// get api access token
$api_credentials = base64_encode($api_key.':'.$api_secret);
$auth_headers = 'Authorization: Basic '.$api_credentials."\r\n".
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'."\r\n";
$auth_context = stream_context_create(
array(
'http' => array(
'header' => $auth_headers,
'method' => 'POST',
'content'=> http_build_query(array('grant_type' => 'client_credentials', )),
)
)
);
$auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true);
$auth_token = $auth_response['access_token'];
// get tweets
$data_context = stream_context_create( array( 'http' => array( 'header' => 'Authorization: Bearer '.$auth_token."\r\n", ) ) );
$data = json_decode(file_get_contents($data_url.'?count='.$data_count.'&screen_name='.urlencode($data_username), 0, $data_context), true);
// result
// print('<pre>');
// print_r($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment