John Davidson

php - Twitter stream api print out statuses

0 comments
Message:


I have the following code


<?php
/*
This is an app to search tiwtter statuses.
*/


function queryTwitter($search)
{
$url = "https://api.twitter.com/1.1/search/tweets.json";
if($search != "")
$search = "#".$search;
$query = array( 'count' => 100, 'q' => urlencode($search), "result_type" => "recent");
$oauth_access_token = "XXXX";
$oauth_access_token_secret = "xxxx";
$consumer_key = "xxxx";
$consumer_secret = "xxxx";

$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');

$base_params = empty($query) ? $oauth : array_merge($query,$oauth);
$base_info = buildBaseString($url, 'GET', $base_params);
$url = empty($query) ? $url : $url . "?" . http_build_query($query);

$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;

$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return json_decode($json);
}

function buildBaseString($baseURI, $method, $params)
{
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}

function buildAuthorizationHeader($oauth)
{
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}




// This is where I want to break down the object to an array and have it print out each individual tweet
function displayTweets($object){

$myArray = json_decode(json_encode($object), true);

//print_r($myArray);

foreach ($myArray as $tweet){

print "Status: ";
$array = print_r($tweet,true);
print $array['text'];
print "<br>";
}




}



?>

<html>
<head>

</head>

<body>

Search here for twitter statuses.

<input type='text'>

<br>




<?php

$search = queryTwitter("dbz");

//print_r($search);
displayTweets($search);






?>


</body>


</html>

I am trying to put out a status found like this...
print $tweet['text'];


I am not sure on how to convert the $search object to an array where I can print $tweet['text'] or print $tweet['location'];


How do I convert the object created by the function queryTwitter($search) to a printable array. I also tried to foreach the object and print out $tweet->text and it didn't work. When I use print_r($object) it prints out the information. How can I complete the displayTweets function?



My Answer:
php

require_once('TwitterAPIExchange.php');

$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=twitterapi&count=20';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

$statuses = json_decode($response, true);

foreach ($statuses as $status) {
echo $status['text'] . "\n";
}

?>


This code uses the TwitterAPIExchange library to make a request to the Twitter API and retrieve the user timeline statuses for the specified screen name. It then decodes the JSON response and prints out the text of each status. Make sure to replace the placeholders for the OAuth access token, access token secret, consumer key, and consumer secret with your own credentials.

Rate this post

5 of 5 based on 1911 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us