Getting JSON data from Twitter using ID in PHP - php

I have a following script which helps me getting data in json format from a particular Facebook ID.
Here's the code
if($recipy_info[0]['social_media']=='facebook')
{
$pageContent = file_get_contents('https://graph.facebook.com/'.$recipy_info[0]['post_userid'].'?fields=id,name,picture,birthday');
$parsedJson = json_decode($pageContent);
//$facebook_info= https://graph.facebook.com/100003384367738?fields=id,name,picture,birthday
}
Now the issue is that I want to have a similar work for Twitter, I don't have screen name, all I have is just the id 1897279429.
I have a url https://api.twitter.com/1.1/trends/1897279429.json, but it gives me this error:
{"errors":[{"message":"Bad Authentication data","code":215}]}
How can I sort my problem?
I want to get JSON data from the id I have.

Since API 1.1 you are required to authenticate first before you can get the public tweets from a user. Please take a look at the API documentation here: https://dev.twitter.com/docs

Related

Getting Twitch followers using Twitch API in PHP

What I'm trying to do is make a twitch follower alert in PHP. The only stuff so far that I have gotten done is find out where to get the info from. I need assistance decoding the info, and then getting the user name and setting it to a string. The place for the recent followers is: https://api.twitch.tv/kraken/channels/trippednw/follows/?limit=1
The data Twitch's API gives you is in JSON format (JavaScript Object Notation). You can decode it using json_decode($data, true). This gives you an associative array with the required fields. For example, to get the name of the user who most recently followed:
json_decode($twitch_data, true)['follows'][0]['user']['name']
Update:
Here's a more detailed answer. First, you're going to have to get the data from the Twitch API via a get request using file_get_contents(). Then, you parse the resulting JSON using the method described above and echo that out onto the page. Here's the full code:
<?php
$api_url = 'https://api.twitch.tv/kraken/channels/trippednw/follows/?limit=1';
$api_data = file_get_contents($api_url);
$last_follow = json_decode($api_data, true)['follows'][0]['user']['name'];
echo "The last user to follow <strong>trippednw</strong> on Twitch is <strong>" . $last_follow . "</strong>.";
?>

Get my own full profile with LinkedIn API

For testing purposes, I'd like to get my own full profile datas from LinkedIn API.
So far my code looks like this :
// Fill the keys and secrets you retrieved after registering your app
$oauth = new OAuth("APIKEY", "SECRETKEY");
$oauth->setToken("Token OAuth", "Secret User OAuth");
$oauth->disableSSLChecks();
$params = array();
$headers = array();
$method = OAUTH_HTTP_METHOD_GET;
// Specify LinkedIn API endpoint to retrieve your own profile
$url = "https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,location:(name),skills:(name),educations:(id,school-name,field-of-study))?format=json";
// By default, the LinkedIn API responses are in XML format. If you prefer JSON, simply specify the format in your call
// $url = "https://api.linkedin.com/v1/people/~?format=json";
// Make call to LinkedIn to retrieve your own profile
$oauth->fetch($url, $params, $method, $headers);
$oProfile = json_decode($oauth->getLastResponse());
var_dump($oProfile);
Although I am getting basic profile informations (firstName,headline etc...) but when it comes to full profile informations I get an object with '...' as value everytime, although the informations exist.
I have r_fullprofile ticked in my LinkedIn app interface, so I don't know what I have to do to get these values.
I tried your query with my own account. It looks you issue is with the skills field.
You can see in the LinkedIn API documentation that skills are made up of a skill, and each skill has a name. If you only want the name returned the proper way to ask for it is
skills:(skill:(name)), whereas your request asks for skills:(name).
Here is an updated request for you:
GET https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,location:(name),skills:(skill:(name)),educations:(id,school-name,field-of-study))?format=json

Facebook PHP API not always returning event venue data

I have 3 events:
https://www.facebook.com/491594114271958 (Mad Haus)
https://www.facebook.com/569226999799343 (Deuglish)
539504962802119/ (Piffle)
All are being fetched via the PHP
$config = array();
$config['appId'] = $appId;
$config['secret'] = $secret;
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$ev = $facebook->api('/'.$id."?fields=cover,description,location,name,owner,venue",'GET');
print_r($ev);
For some reason Mad Haus and Piffle do not return venue data but Deuglish does. All events return basic data such as title, description and start time. When I run them through the Graph API explorer the venue data is returned as expected but not through the PHP API, any ideas? I can not for the life of me see the difference with these 3 events.
Thanks,
Caroline
Well, if you don't want to wait for the Facebook developers to fix the bug, you can try the same by making a simple GET request (of course along with an Access Token) to Graph API using PHP cURL. Since you are able to retrieve the venues while using the Graph API explorer, I'm pretty sure you'll be able to get them by making a simple GET request using PHP cURL.
This however involves an overhead of parsing the received JSON object and dealing with errors will be a little difficult here. But you can give it a shot.

unable to retrieve picasa photos with php when requesting data feed

I tried to retrieve the latest 10 photos from my picasa account but it doesn't work.
$file = file_get_contents("http://picasaweb.google.com/data/feed/api/user/firdawsfm?kind=photo&max-results=10&alt=json&access=public&thumbsize=".$tSize);
print_r($file);
the result :
{"$t":"http://picasaweb.google.com/data/feed/api/user/firdawsfm"},"updated":{"$t":"2013-09-08T19:27:11.010Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind",
"term":"http://schemas.google.com/photos/2007#user"}],
"title":{"$t":"108451527358440546192","type":"text"},
"subtitle":{"$t":"","type":"text"},
"icon":{"$t":"http://lh3.ggpht.com/-Srl88atqmQE/AAAAAAAAAAI/AAAAAAAAAAA/AhcCTIASEAM/s64-c/108451527358440546192.jpg"},
"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml",
"href":"http://picasaweb.google.com/data/feed/api/user/108451527358440546192?alt=json"},{"rel":"alternate",
"type":"text/html",
"href":"https://picasaweb.google.com/108451527358440546192"},{"rel":"http://schemas.google.com/photos/2007#slideshow",
"type":"application/x-shockwave-flash",
"href":"https://static.googleusercontent.com/external_content/picasaweb.googleusercontent.com/slideshow.swf?host=picasaweb.google.com&RGB=0x000000&feed=http://picasaweb.google.com/data/feed/api/user/108451527358440546192?alt%3Drss"},{"rel":"self","type":"application/atom+xml",
"href":"http://picasaweb.google.com/data/feed/api/user/108451527358440546192?alt=json&q=&start-index=1&max-results=10&kind=photo&thumbsize=180c&access=public"}],
"author":[{"name":{"$t":"Firdaws Haskell"},"uri":{"$t":"https://picasaweb.google.com/108451527358440546192"}}],
"generator":{"$t":"Picasaweb",
"version":"1.00",
"uri":"http://picasaweb.google.com/"},
"openSearch$totalResults":{"$t":0},
"openSearch$startIndex":{"$t":1},"openSearch$itemsPerPage":{"$t":10},
"gphoto$user":{"$t":"108451527358440546192"},"gphoto$nickname":{"$t":"Firdaws Haskell"},"gphoto$thumbnail":{"$t":"http://lh3.ggpht.com/-Srl88atqmQE/AAAAAAAAAAI/AAAAAAAAAAA/AhcCTIASEAM/s64-c/108451527358440546192.jpg"}}}
there is no data about photos. when I tried this exemple with another account it works. I verified the photos are public.
I tried your url and all works fine, i can access gphoti$id and media$group values.
So far seems all ok ;) Try again!
Maybe you didn`t have public photos at that time there...
Not relevant answer to that question
(in case if server requests authorisation):
For all Picasa web albums api queries with alt=json, or alt=json-in-code and /userid/default/ you must provide access_token parameter.
Access token you can get using OAuth2 authorization work-flow as described here:
http://code.google.com/p/google-api-php-client/wiki/OAuth2 (using google-api-php-client SDK for example)
And using in scopes this value "http://picasaweb.google.com/data/".
More how to do OAuth2 and get access token from https://accounts.google.com/o/oauth2/token
after requesting user login: https://accounts.google.com/o/oauth2/auth you can find on official website:
https://developers.google.com/accounts/docs/OAuth2Login
In final you must have:
$file = file_get_contents("http://picasaweb.google.com/data/feed/api/user/firdawsfm?kind=photo&max-results=10&alt=json&access=public&thumbsize=".$tSize."&access_token=".$access_token);
print_r($file);

Get wall feed from a public Facebook page using Graph API - is it really this complex?

I have started off by reading Displaying Facebook posts to non-Facebook users which is of some use but I cannot believe it is this difficult to get a public feed from Facebook.
The page I want a feed from is public, you do not need to be logged into get to it.
Am I right in presuming that I need an access_token to get to this information, attempting to access the URL without results in an OAuth error.
So the flow should be like this (massively, overly complex):
Authenticate using a user (what if the user isn't on Facebook?)
Some complex OAuth nonsense - just to read the feed, I do not even want a like button or post to wall functionality
Get the feed using a PHP request to the correct URL with the user's access_token
Render the feed
Assuming the user isn't on Facebook, what do you do, use a generic app to get the feed?
Hardcode an auth request to Facebook using my generic app's ID and secret
Some complex OAuth nonsense
Get the feed using a PHP request to the correct URL with the app's access_token
Render the feed
Oh no, the auth has expired, re-auth and capture this new access_token for use in future requests.
This seems really complex for no reason other than Facebook wants to know EVERYTHING that is going on, it'd be easier to do a cURL and scrape the content from the public URL using XPath.
Any help on this would be great.
Thanks,
Jake
EDIT
An edit to show this is not an exact duplicate.
I had this working with an access_token in place, but now it fails, the token has expired and I can no longer use it to obtain information from the public wall.
I attempted to extend the expiration date of this token using the methods mentioned in other posts but this didn't work and the expiration was not extended - we are now here, with an invalid token and no further along.
It seems that the manual process of having to approve the OAuth request means that it is impossible to programatically get the feed of a public page.
Two years later, you can programmatically do this with a Facebook App (an example using PHP and Slim): https://developers.facebook.com/apps/
$base_api="https://graph.facebook.com/";
$client_id="XXXXXX";
$app_secret="XXXXXX";
//get a profile feed (can be from a page, user, event, group)
$app->get('/feed/:profileid/since/:start_date', function ($profile_id,$start_date) {
$start_time=date('m/d/Y h:i:s',$start_date);
$request = new FacebookRequest(
getSession(),
'GET',
'/'.$profile_id.'/feed?since='.$start_time
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
//do something with $graphObject
});
function getSession(){
$session = new FacebookSession(getAccessToken());
return $session;
}
function getAccessToken(){
global $base_api, $client_id, $app_secret;
$url=$base_api."oauth/access_token?client_id=".$client_id."&client_secret=".$app_secret."&grant_type=client_credentials";
$str = file_get_contents($url);
$token = str_replace ( "access_token=" , "" , $str );
return $token;
}
I have some success with reading in the direct feed without tokens etc.
(using magpie, simplepie or querypath or similar).
http://www.facebook.com/feeds/page.php?format=rss20&id=........
http://www.facebook.com/feeds/page.php?format=atom10&id=........
found on: http://ahrengot.com/tutorials/facebook-rss-feed/
Facebook has changed how to retrieve a public Facebook page's feed since the other answers were posted.
Check out my answer/question. It's not PHP, but it provides the URLs and process you need.

Categories