how to apply CSS / HTML in twitter API JSON results - php

using below code, TWITTER API and PHP, i have successfully fetch/get my twitter timeline.
$url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
$getfield = '?screen_name=twitter';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
my concern is, the data/results displayed fills up my whole window screen with text. im not sure how to apply CSS or HTML on it.
[{"created_at":"Sun Dec 09 05:21:29 +0000 2012","id":2232376342522313442,"id_str":"277231523253442","text":"http:\/\/t.co\/ro74jh4","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"
the tutorial im following didn't mentioned how to arrange the fetched data.

You'll need to parse the JSON data that you got from object from Twitter. Here is an example. You can use the array syntax to find which values you want to look up.
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$obj = json_decode($response, true);
echo "<div><ul>";
foreach ($obj["statuses"] as $index => $result) {
$tweet = $result['text'];
$user = $result['user']['screen_name'];
$profile_image = $result['user']['profile_image_url'];
echo "<li>";
echo "<img src=\"".$profile_image."\" width=\"25px\" height=\"25px\" />";
echo "#$user";
echo " $tweet";
echo "</li>";
}
echo "</ul></div>";

Related

Problems with looping a json

Here I have a json which i get after making a request to the twitter API. I have already seen different working examples of looping a json. Even I have do it in another projects. But this time it isn't working.
I've tested a lot of things. I guess it's normal it doesn't work for now. The variable arrayFriends isn't even being used. Just i was testing some moves.
Don't know exactly where I am failing. I would like to have some feedback
Thank you guys!
$url = 'https://api.twitter.com/1.1/friends/ids.json';
$getfield = '?screen_name=J7mb0';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
$arrayFriends = var_dump(json_decode($json, true, 512, JSON_BIGINT_AS_STRING));
$json = json_decode($json, true);
echo $json;
foreach($json->ids as $obj){
echo $obj->ids;
}
If you're using json_decode with true, the returned value will be an array, not an object.
Therefore, you won't be able to use the property-access method here:
foreach($json->ids as $obj){
You have to use the array-access method instead:
foreach($json['ids'] as $obj){
or, change the parameter at decoding, so you get an object:
$json = json_decode($json, false);
// or just simply:
// $json = json_decode($json);
// since the second parameter defaults to FALSE
ALSO, $arrayFriends will remain empty, because var_dump returns nothing at all.
// Change this:
// $arrayFriends = var_dump(json_decode($json, true, 512, JSON_BIGINT_AS_STRING));
// ...to this:
$arrayFriends = json_decode($json, true);
It worked! After trying different things it started working.
Actually I'm trying to access another json but this isn't working. It is the same app but with more content.
$user = 'seyroku';
$url = 'https://api.twitter.com/1.1/friends/ids.json';
$getfield = '?screen_name='.$user.'';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
$arrayFriends = json_decode($json, true,512,JSON_BIGINT_AS_STRING);
foreach($arrayFriends['ids'] as $obj){
echo $obj,",";
$url2 = 'https://api.twitter.com/1.1/users/lookup.json';
$getfield2 = '?user_id='.$obj.'';
$json2 = $twitter->setGetfield($getfield2)
->buildOauth($url2, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
echo $twitter->setGetfield($getfield2)
->buildOauth($url2, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
$json3 = json_decode($json2, true);
foreach ($json3['name'] as $nombre) {
echo $nombre,",";
}
/* $sql = "INSERT INTO friendtest (user, friends)
VALUES ('$user' , '$obj' )";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}*/
}
The first for each it returns me the id properly. But the second one which I expect to give me the name of that id doesn't do it. I tried different things but still isn't working.
Could you give me some feedback?
Sorry guys for being so bad at this.

How to display an image in PHP given the url of the image? (PHP NOOB)

So what I'm doing is using the Twitter RestAPI to get the picture of a user and just be able to display the image on a local page. I've figured out the way to grab the URL of the image and this is what I have so far:
$url = $result->profile_image_url;
This holds the url of the profile picture of the user. Now how do I use this url to display it on the page? I know HTML allows us to do something like :
<img src = "some url">
And it will display the picture on the web page. I've looked around the internet for a while now, and still haven't figured it out. I've run into the same suggestion of the following :
<img src = "<?php echo ($url); ?>"/>
But I keep getting a parse error when I attempt loading the page. I would really appreciate if someone could guide me/help me figure out what's going on. Clearly, I'm doing something wrong which is why the PHP interpreter is not able to parse the code. Thanks!
Here is my whole code (if that helps):
<html>
<body>
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "N/A",
'oauth_access_token_secret' => "N/A",
'consumer_key' => "N/A",
'consumer_secret' => "N/A"
);
$url = 'https://api.twitter.com/1.1/users/show.json';
$getfield = '?screen_name=kobebryant';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$result = json_decode($json);
$url = $result->profile_image_url;
<img src = "<?php echo ($url);?>"/> //This is the line giving me an error
?>
</body>
</html>
You closed the php tag too late. That is why it is recommended to split php from html. Try this code below:
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => 'N/A',
'oauth_access_token_secret' => 'N/A',
'consumer_key' => 'N/A',
'consumer_secret' => 'N/A'
);
$url = 'https://api.twitter.com/1.1/users/show.json';
$getfield = '?screen_name=kobebryant';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$result = json_decode($json);
$url = $result->profile_image_url;
?>
<html>
<head></head>
<body>
<img src="<?php echo ($url);?>"/> //This is the line giving me an error
</body>
</html>
Hint: if you've just a string without variables use ' instead of " because php tries to parse content between " and this is waste of CPU power.

How to print Twitter API request reply?

I want to print all the tweets from the reply. What am I missing?
https://github.com/J7mbo/twitter-api-php
<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=%23twitter';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
foreach ($response->statuses as $tweet) {
echo $tweet->text;
}
Notice: Trying to get property of non-object in
/var/www/projects/twitter/twitter-api-php/index.php on line 23
Warning: Invalid argument supplied for foreach() in
/var/www/projects/twitter/twitter-api-php/index.php on line 23
The performRequest() method returns a JSON string, not an array, so you need to decode it $array = json_decode($response, true);.
You just have to use:
`echo $response;`
or instead:
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
but I recommend using the first approach.

Parsing JSON response from Twitter Search API not working using PHP

I am using the Twitter Search API 1.1 to search for tweets on a particular keyword and display the results as a search results page at motherpipe.co.uk.
I have managed to get the Oauth working with the help of James Mallison at http://github.com/j7mbo/twitter-api-php.
I am stuck at the stage of taking the actual response I get from Twitter and display it normally in divs in HTML (using PHP). This is the reply I get: https://motherpipe.co.uk/staging/index2.php.
I just can't seem to write a simple loop to display only the 'screen_name' and 'text'.
So far I have tried different versions of this:
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=sweden&result_type=recent';
$twitter = new TwitterAPIExchange($settings);
echo $twitter ->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($twitter);
foreach($response as $tweet)
{
echo "{$tweet->screen_name} {$tweet->text}\n";
}
Any feedback or ideas on how to loop through this correctly would be much appreciated.
Cheers
I dont use james code but I get it working other way round
<ul>
<?php $get_tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
foreach($get_tweets as $tweet) {
$tweet_desc = $tweet->text;
?>
<li><?php echo $tweet_desc ?></li>
<?php }?>
</ul>
You can see a working Twitter Api V1.1 at the footer of this page. If you want something like this or further customisation let me know .
If you would do a var_dump of $response, you would probably get a better idea of the structure. Looks like you need to loop over $response->statuses. The user information is under $tweet->user->screen_name and the tweet text is under $tweet->text. So:
$response = json_decode($twitter);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}\n";
}
The user information is under $tweet->user->screen_name and the tweet text is under $tweet->text
$response = json_decode($twitter);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}<br />";
}
OR also you can use this too
$response = json_decode($twitter, true);
$counter = 0;
foreach($response['statuses'] as $tweet)
{
echo "{$tweet[$counter]['user']['screen_name'] {$tweet[$counter]['text']}<br />";
$counter +=1;
}
So the answer (thanks to Prisoner, Jonathan Kuhn and Mubin Khalid) is, in order to display the response from the Twitter Search API, assign a variable to the Twitter JSON response. This will enable you to loop through and display whatever you want from the response.
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=sweden&result_type=recent';
$twitter = new TwitterAPIExchange($settings);
$api_response = $twitter ->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($api_response);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}\n";
}

Iterating over a JSON object in PHP

How do I grab - or more specifically, iterate over - the contents of a JSON user_timeline object in PHP? I have it retrieved, I'm just trying to access the fields in the object. All of the other tutorials/answers don't apply to the new 1.1 API. Here's where I am:
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=testyMcTesterton';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
If you posted the contents of the object itself it would help, but can you not just json_decode($obj)? From that do something like:
$new = json_decode($obj);
$data = $new->data; // (this would access Object->data->*)
That's how a JSON array is accessed, though I don't actually know how the Twitter user_timeline object looks so I can't tell you exactly how to access the elements you want specifically.

Categories