I am trying to get and array of more then 900 ids in this way, but I get an error message.
$url = 'https://graph.facebook.com/?ids=' . implode(',', $array);
$result = file_get_contents($url);
I think because the list is too big I got this error - if I shorten the list facebook gets me the JSON.
Is there any suggestion to get it in another fashion? (maybe batch call, or something like curl? )
The only other way is to implement a batch call with smaller API calls.
https://developers.facebook.com/docs/graph-api/making-multiple-requests
Related
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>.";
?>
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.
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
For a project I have to grab the insights of a page over a long period of time (e.g. 1-2 years) of facebook.
I first tried to do a single request but it turned out that only requesting
/PAGE_ID/insights?since=xxx&until=xxx
doesn't return all the data I want (it somehow supresses data as if there's some limit to the size of the answer).
I then tried to split up the date range (e.g. 01.04.2011-01.04.2011 -> 01.04.2011-01.08.2011-01.12.2011-01.04.2011) which as well, didn't quite work like I wanted it to.
My next approach was to request only the insight values I need, like 'page_stories, page_impressions, ...'. The requests looked like this
/PAGE_ID/insights/page_impressions/day?since=xxx&until=xxx
This actually worked but not with ajax. It sometimes seemed to drop some requests (especially if I changed the browser tab in google chrome) and I need to be sure that all requests return an answer. A synchronus solution would just take way too much time considering that one requests needs at least 2 seconds and with a date range of 2 years I may have about 300 single requests this just takes way too long to complete.
Lastly I stumbled over facebook ability to do batch requests which is exactly what I need. It can pack up to 50 requests in one call which significantly lowers the bandwith. And here's where I'm stuck. The facebook api gives some examples on how to use it but none of them worked when I tested them in the Graph Explorer and via the php facebook api sdk. I tried to pack this request
PAGE_ID/insights/page_fan_adds/day?since=1332486000&until=1333695600
into a batch request but failed.
It seems that the api is bugged. It's always giving me this error when I'm using a question mark '?' in the 'relative_url' field.
{
"error": {
"message": "batch parameter must be a JSON array",
"type": "GraphBatchException"
}
}
Here is what I tried:
These give the 'must be a JSON array' error:
?batch=[{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day?since=1332486000&until=1333695600"}]
These two actually return data but they are ignoring the parameters:
?batch=[{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":"since=1332486000 until=1333695600"}]
?batch=[{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":"since=1332486000,until=1333695600"}]
?batch=[{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":{"since":"1332486000","until":"1333695600"}}]
And this one tells me that this is an 'Unsupported post request':
?batch=[{"method":"POST","relative_url":"/PAGE_ID/insights/page_fan_adds/day","body":"since=1332486000 until=1333695600"}]
Can someone help?
I finally found the solution to my problem. It's not mentioned in the facebook documentation but for this request
?batch=[{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day?since=1332486000&until=1333695600"}]
to properly work we have to use a function like
urlencode()
to encode the json part. This way the querys work like a charm. A php example:
$insights = $facebook->api('?batch=['.urlencode('{"method":"GET","relative_url":"/PAGE_ID/insights/page_fan_adds/day?since=1332572400&until=1333782000"}').']'
,'post',array('access_token' => $this->facebook->getAccessToken()));
which results in this:
?batch=[%7B%22method%22%3A%22GET%22%2C%22relative_url%22%3A%22%2FPAGE_ID%2Finsights%2Fpage_fan_adds%2Fday%3Fsince%3D1300086000%26until%3D1307862000%22%7D]
This example is for using an array of IDs to make a batch request with urlencoding.
$postIds = [
'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
'XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXX',
];
$queries = [];
foreach( $postIds as $postId ) {
$queries[] = [
'method' => 'GET',
'relative_url' => '/' . $postId . '/comments?summary=1&filter=stream&order=reverse_chronological',
];
}
$requests = $facebook->post( '?batch=' . urlencode( json_encode( $queries ) ) )->getGraphNode();
I'm trying to get nearby venues. I found this url where i can just plug in lat and lon.
How do i use this URL? I tried getting the libraries from Foursquare API site, but can't get them to work.
I know i can json_decode the result on this page to get a php object, but how do i make a call and get the data to my webpage?
https://api.foursquare.com/v1/venues.json?geolat=40.562362&geolong=-111.938689&l=10
Thanks
$data = file_get_contents(http://MyURL/);
$results = json_decode($data, true); //true = making it into array, not object