I've a (i hope small) porblem. I'm building a twitter bot with TwitterAPIExchange.php, but if I run the script the hole time I only see array
on my screen. what do I wrong? here is my twitter.php code:
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'consumer_key' => "xxx",
'consumer_secret' => "xxx",
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=kamerwatch';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(), true);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p> <em>".$string[errors][0]["message"]."</em></p>";exit();}
echo $string;
foreach($string as $items)
{
echo "Screen name: ". $items['user']['screen_name']."<br />";
echo "Tweet: ". $items['text']."<br />";
echo "Time and Date of Tweet: ".$items['created_at']."<br />";
echo "Tweet ID: ".$items['id_str']."<br />";
echo "Followers: ". $items['user']['followers_count']."<br /><hr />";
}
?>
code at the moment:
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'consumer_key' => "xxx",
'consumer_secret' => "xxx",
'oauth_access_token' => "xxxx",
'oauth_access_token_secret' => "xxx"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=kamerwatch&count=3'; /* added only 3 results for testing purpose */
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$string = json_decode($response, true);
#print_r($string); /* outputs what follows */
foreach ($string as $input => $items){
$userscreen_name = $items['user']['screen_name'];
$text = $items['text'];
$created_at = $items['created_at'];
$id_str = $items['id_str'];
$followers_count = $items['user']['followers_count'];
echo "Screen name:$userscreen_name<br />";
echo "Tweet: $text<br />";
echo "Time and Date of Tweet: $created_at<br />";
echo "Tweet ID: $id_str<br />";
echo "Followers: $followers_count<br />";
};
?>
This works fine when I test it :
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'consumer_key' => "xxx",
'consumer_secret' => "xxx",
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx"
); /* don't forget to add your params ^^ */
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=kamerwatch&count=3'; /* added only 3 results for testing purpose */
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$string = json_decode($response, true);
print_r($string); /* outputs the response */
/* below is the code to fetch through the array */
/* please note that your response array starts with -> Array ( [statuses] => Array ( [0] */
/* so we need to specify : $string['statuses'] to access the fields */
foreach($string['statuses'] as $items) {
$datecreate = $items['created_at'];
$id = $items['id'];
$twit = $items['text'];
echo "ID: $id<br />";
echo "Created: $datecreate<br />";
echo "Tweet: $twit<br />";
}