Twitter API v 1.1 code in PHP - php

I am using twitter API v1.1 to get tweets in my web application the code for getting tweets is as
$name = 'zkalvi';
$url = "https://api.twitter.com/1.1/search/tweets.json/".$name;
$tweets = json_decode(file_get_contents($url));
echo '<ul>';
foreach($tweets as $tweet) {
echo '<li>'.$tweet->text.'</li>';
}
echo '</ul>';
and $name = 'zkalvi';
$url = "https://api.twitter.com/1.1/search/tweets.json/".$name;
$tweets = simplexml_load_file($url);
echo '<ul>';
foreach($tweets as $tweet) {
echo '<li>'.$tweet->text.'</li>';
}
echo '</ul>';
both of these codes are not showing the tweets.
Please help me why the tweets are not showing and how could I show the tweets?
Thanks in advance.

first of all, the way you are calling is wrong, your url should be like this
https://api.twitter.com/1.1/search/tweets.json?q=zkalvi.
By this url you will get following output
{"errors":[{"message":"Bad Authentication data","code":215}]}
This is because , in twitter 1.1 version, most of thing needs authentication. So better you use twitter server side library for getting results.
Here is link for twitteroauth library for php
twitteroauth

Related

Grab/Fetch Facebook comments through query.using Graph API

So I was trying to fetch facebook comments then put it into my own website so then I could reply it through my website. After doing some research, I found it Graph API could be the solution to my problem. Below is the code
<?php
$group_id ="14********_1**********99";
$access_token="accesstoken";
$url = "https://graph.facebook.com/v2.11/" . $group_id .
"/comments?
fields=message,from{name,id}&access_token=".$access_token;
$json= file_get_contents($url);
$data = json_decode($json);
foreach ($data->comments->data as $key) {
if(isset($key->message))
{
echo $key->message;
echo "<br>";
}
}
?>
How do I fetch comments from Facebook through the URL above? Sorry I'm still new to programming.

Google contacts api parsing xml and get contact title

I'm using oauth 2.0 and I trying to use contacts google api.
$url = 'https://www.google.com/m8/feeds/contacts/default/full?v=2.0&max-results='.$max_results.'&oauth_token='.$accesstoken;
Result of this I have there: $xmlresponse result is fine and good (it means that not have issues)
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('atom', 'http://schemas.google.com/g/2005#kind');
$result = $xml->xpath('//atom:title');
var_dump($xml);
var_dump($result);
foreach ($result as $title) {
echo $title->attributes()->address . "<br>";
}
I want to get title contact (in var_dump($xml) it is visible as 'title') but $result is empty
Question:
1) How to get title contact from this xml response ?

How extract information from a json retrieved from an API?

I'm trying to get somes informations from an API but I always failed.
The API code :
{"matches":[{"matchId":2032325149,"region":"EUW","platformId":"EUW1","matchMode":"CLASSIC","matchType":"MATCHED_GAME","matchCreation":1427400448291,"matchDuration":2225,"queueType":"RANKED_SOLO_5x5","mapId":11,"season":"SEASON2015","matchVersion":"5.6.0.190","participants":[{"teamId":100,"spell1Id":3,"spell2Id":4,"championId":53,"highestAchievedSeasonTier":"UNRANKED","timeline":{"creepsPerMinDeltas":{"zeroToTen":0.9,"tenToTwenty":0.9,"twentyToThirty":0.6,"thirtyToEnd":1.2},"xpPerMinDeltas":{"zeroToTen":253.60000000000002,"tenToTwenty":237.8,"twentyToThirty":477.29999999999995,"thirtyToEnd":448.4},"goldPerMinDeltas":{"zeroToTen":128.2,"tenToTwenty":169.6,"twentyToThirty":310.9,"thirtyToEnd":211.2},"csDiffPerMinDeltas":{"zeroToTen":-0.8,"tenToTwenty":-0.5499999999999998,"twentyToThirty":-1.2499999999999998,"thirtyToEnd":-0.30000000000000027},"xpDiffPerMinDeltas":{"zeroToTen":-12.299999999999983,"tenToTwenty":-127.04999999999998,"twentyToThirty":120.09999999999997,"thirtyToEnd":-491.9},"damageTakenPerMinDeltas":{"zeroToTen":187.2,"tenToTwenty":644.0999999999999,"twentyToThirty":1015.2,"thirtyToEnd":2076.2},"damageTakenDiffPerMinDeltas":{"zeroToTen":9.149999999999963,"tenToTwenty":32.49999999999997,"twentyToThirty":126.24999999999991,"thirtyToEnd":762.9000000000001},"role":"DUO_SUPPORT","lane":"BOTTOM"},"masteries":[{"masteryId":4211,"rank":2},{"masteryId":4213,"rank":2},{"masteryId":4221,"rank":1},{"masteryId":4222,"rank":3},{"masteryId":4232,"rank":1},{"masteryId":4233,"rank":3},{"masteryId":4234,"rank":3},{"masteryId":4244,"rank":1},{"masteryId":4312,"rank":3},{"masteryId":4313,"rank":3},{"masteryId":4322,"rank":1},{"masteryId":4324,"rank":1},{"masteryId":4331,"rank":3},{"masteryId":4334,"rank":1},{"masteryId":4341,"rank":1},{"masteryId":4342,"rank":1}],"stats":{"winner":false,"champLevel":15,"item0":2049,"item1":3401,"item2":3270,"item3":2010,"item4":3110,"item5":3082,"item6":3340,"kills":1,"doubleKills":0,"tripleKills":0,"quadraKills":0,"pentaKills":0,"unrealKills":0,"largestKillingSpree":0,"deaths":11,"assists":10,"totalDamageDealt":31311,"totalDamageDealtToChampions":9198,"totalDamageTaken":31747,"largestCriticalStrike":0,"totalHeal":3600,"minionsKilled":36,"neutralMinionsKilled":0,"neutralMinionsKilledTeamJungle":0,"neutralMinionsKilledEnemyJungle":0,"goldEarned":8264,"goldSpent":8055,"combatPlayerScore":0,"objectivePlayerScore":0,"totalPlayerScore":0,"totalScoreRank":0,"magicDamageDealtToChampions":7159,"physicalDamageDealtToChampions":2038,"trueDamageDealtToChampions":0,"visionWardsBoughtInGame":0,"sightWardsBoughtInGame":0,"magicDamageDealt":22000,"physicalDamageDealt":5572,"trueDamageDealt":3737,"magicDamageTaken":15037,"physicalDamageTaken":16608,"trueDamageTaken":102,"firstBloodKill":false,"firstBloodAssist":false,"firstTowerKill":false,"firstTowerAssist":false,"firstInhibitorKill":false,"firstInhibitorAssist":false,"inhibitorKills":0,"towerKills":0,"wardsPlaced":28,"wardsKilled":1,"largestMultiKill":1,"killingSprees":0,"totalUnitsHealed":4,"totalTimeCrowdControlDealt":445},"participantId":0,"runes":[{"runeId":5273,"rank":9},{"runeId":5289,"rank":9},{"runeId":5317,"rank":9},{"runeId":5357,"rank":3}]}],"participantIdentities":[{"participantId":0,"player":{"summonerId":66366116,"summonerName":"name","matchHistoryUri":"/v1/stats/player_history/EUW1/217404427","profileIcon":690}}]}]}
I'm trying to get "Kills" value and this is my php code :
<?php
$result = file_get_contents('API URL LINK HERE);
$key = json_decode($result);
print '<p><b>' . $key->matches->participants->stats->Kills . '</b></p>';
?>
Please can someone correct my code ?
Thanks a lot.
<?php
$json = '{"matches":[{"matchId":2032325149,"region":"EUW","platformId":"EUW1","matchMode":"CLASSIC","matchType":"MATCHED_GAME","matchCreation":1427400448291,"matchDuration":2225,"queueType":"RANKED_SOLO_5x5","mapId":11,"season":"SEASON2015","matchVersion":"5.6.0.190","participants":[{"teamId":100,"spell1Id":3,"spell2Id":4,"championId":53,"highestAchievedSeasonTier":"UNRANKED","timeline":{"creepsPerMinDeltas":{"zeroToTen":0.9,"tenToTwenty":0.9,"twentyToThirty":0.6,"thirtyToEnd":1.2},"xpPerMinDeltas":{"zeroToTen":253.60000000000002,"tenToTwenty":237.8,"twentyToThirty":477.29999999999995,"thirtyToEnd":448.4},"goldPerMinDeltas":{"zeroToTen":128.2,"tenToTwenty":169.6,"twentyToThirty":310.9,"thirtyToEnd":211.2},"csDiffPerMinDeltas":{"zeroToTen":-0.8,"tenToTwenty":-0.5499999999999998,"twentyToThirty":-1.2499999999999998,"thirtyToEnd":-0.30000000000000027},"xpDiffPerMinDeltas":{"zeroToTen":-12.299999999999983,"tenToTwenty":-127.04999999999998,"twentyToThirty":120.09999999999997,"thirtyToEnd":-491.9},"damageTakenPerMinDeltas":{"zeroToTen":187.2,"tenToTwenty":644.0999999999999,"twentyToThirty":1015.2,"thirtyToEnd":2076.2},"damageTakenDiffPerMinDeltas":{"zeroToTen":9.149999999999963,"tenToTwenty":32.49999999999997,"twentyToThirty":126.24999999999991,"thirtyToEnd":762.9000000000001},"role":"DUO_SUPPORT","lane":"BOTTOM"},"masteries":[{"masteryId":4211,"rank":2},{"masteryId":4213,"rank":2},{"masteryId":4221,"rank":1},{"masteryId":4222,"rank":3},{"masteryId":4232,"rank":1},{"masteryId":4233,"rank":3},{"masteryId":4234,"rank":3},{"masteryId":4244,"rank":1},{"masteryId":4312,"rank":3},{"masteryId":4313,"rank":3},{"masteryId":4322,"rank":1},{"masteryId":4324,"rank":1},{"masteryId":4331,"rank":3},{"masteryId":4334,"rank":1},{"masteryId":4341,"rank":1},{"masteryId":4342,"rank":1}],"stats":{"winner":false,"champLevel":15,"item0":2049,"item1":3401,"item2":3270,"item3":2010,"item4":3110,"item5":3082,"item6":3340,"kills":1,"doubleKills":0,"tripleKills":0,"quadraKills":0,"pentaKills":0,"unrealKills":0,"largestKillingSpree":0,"deaths":11,"assists":10,"totalDamageDealt":31311,"totalDamageDealtToChampions":9198,"totalDamageTaken":31747,"largestCriticalStrike":0,"totalHeal":3600,"minionsKilled":36,"neutralMinionsKilled":0,"neutralMinionsKilledTeamJungle":0,"neutralMinionsKilledEnemyJungle":0,"goldEarned":8264,"goldSpent":8055,"combatPlayerScore":0,"objectivePlayerScore":0,"totalPlayerScore":0,"totalScoreRank":0,"magicDamageDealtToChampions":7159,"physicalDamageDealtToChampions":2038,"trueDamageDealtToChampions":0,"visionWardsBoughtInGame":0,"sightWardsBoughtInGame":0,"magicDamageDealt":22000,"physicalDamageDealt":5572,"trueDamageDealt":3737,"magicDamageTaken":15037,"physicalDamageTaken":16608,"trueDamageTaken":102,"firstBloodKill":false,"firstBloodAssist":false,"firstTowerKill":false,"firstTowerAssist":false,"firstInhibitorKill":false,"firstInhibitorAssist":false,"inhibitorKills":0,"towerKills":0,"wardsPlaced":28,"wardsKilled":1,"largestMultiKill":1,"killingSprees":0,"totalUnitsHealed":4,"totalTimeCrowdControlDealt":445},"participantId":0,"runes":[{"runeId":5273,"rank":9},{"runeId":5289,"rank":9},{"runeId":5317,"rank":9},{"runeId":5357,"rank":3}]}],"participantIdentities":[{"participantId":0,"player":{"summonerId":66366116,"summonerName":"name","matchHistoryUri":"/v1/stats/player_history/EUW1/217404427","profileIcon":690}}]}]}';
$data = json_decode($json, true);
echo $data['matches'][0]['participants'][0]['stats']['kills'];
?>

extraction of tweets an storing

<?php
$mongo = new Mongo();
$db = $mongo->twitter;
$collection = $db->tweets;
$screen_name = "learnmongo";
$twitter = "http://api.twitter.com/1/statuses/user_timeline.json?";
$twitter .= "screen_name=" . $screen_name;
$curl = curl_init($twitter);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$usertimeline = curl_exec($curl);
curl_close($curl);
$usertimeline = json_decode($usertimeline);
foreach ($usertimeline as $id => $item) {
$collection->insert($item);
}
?>
execution of this code gives error message as invalid parameters to foreach().plzz reply and hep me to solve this problem
You are using a deprecated verion of the Twitter API. Version 1.0 no longer works, you need to use version 1.1. You need to use version 1.1 of the call. The API URL has changed from:
http://api.twitter.com/1/statuses/user_timeline.json
to
http://api.twitter.com/1.1/statuses/user_timeline.json
Unfortunately the new 1.1 version requires authentication so your code will still not work until you implement an OAUTH solution for authentication.

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";
}

Categories