Get facebook posts for specific date - php

I have something like this :
$config = array();
$config['appId'] = '';
$config['secret'] = '';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$pageid = "";
// now we can access various parts of the graph, starting with the feed
$pagefeed = $facebook->api("/" . $pageid . "/feed");
i want to get all posts for specific date but i don't know how to do this. If someone can help me it will be great.

You can use time-based pagination. You can read more here (Read Time-based pagination). There is a nice How-To paging with graph API and FQL which can be used for reference.
Here is a simple call that pages through posts on the Chick-fil-A Page:
https://graph.facebook.com/chickfila/posts?limit=5&since={since}&until={until}access_token={access_token}
where
{since} is a Unix timestamp or strtotime data value that points to the start of the range of time-based data.
{until} is a Unix timestamp or strtotime data value that points to the end of the range of time-based data.
{access_token} is users access token.

Related

Requesting User Info with Foursquare API

I am using the Foursquare API to request information for my users.
It says to make a request to get user info like this,
https://api.foursquare.com/v2/users/self?oauth_token=TOKENHERE
So I am doing it like this,
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
When I var_dump I get null from the value I am saving it in, Here is what my full code looks like,
<?php
$client_id = "iwdhwouchweohcuwoehcowehcu";
$secret = "ojdwojwjwrhvo";
$redirect = "http://www.example.com";
if($_GET['code']){
//We need to hit up the authkey URL and get the key in JSON format
$authkey = file_get_contents("https://foursquare.com/oauth2/access_token?client_id=".$client_id."&client_secret=".$secret."&grant_type=authorization_code&redirect_uri=".$redirect."&code=".$_GET['code']);
//We then need to decode it and store that key in a variable
$auth = json_decode($authkey,true);
$access_token = $auth['access_token'];
//we then look up whatever endpoint of the api we want
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
$user = json_decode($fsUser, true);
// $name = $decoded_userinfo['response']['user']['firstName'];
}
?>
When I var_dump $fsUser I get bool(false)
I have var_dump every variable with no issues till I get to $fsUser I can not get past this part...
You also need to use the version number (v) with your request.
Like this
https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&v=20140806&m=foursquare
See documentation here.
https://developer.foursquare.com/overview/versioning

How to get the very first posts of a Group using Facebook Graph API

I have a group on Facebook where the users post funny pictures. I am developing a website that will present the posted pictures in a more organised and interesting way.
My approach is to use Facebook OpenGraph API.
I would like to know how I can obtain the first posts. Eg: the first 10 posts.
By default the graph API (https://graph.facebook.com/{group_id}/feed/) returns the posts sorted from LAST TO FIRST.
I have read the page about Pagination (http://developers.facebook.com/docs/reference/api/pagination/). So, I know about offset and limit parameters.
There appears to be no method of sorting the feed in any other order.
The only approach I can see it to download the whole feed and take what you need...
// Set your access token here...
$accessToken = 'XXX';
// Set your GroupID here...
$groupId = '123';
// Set the number of feed items required here...
$qtyRequired = 10;
$url = 'https://graph.facebook.com/v2.2/' . $groupId . '/feed/?limit=100&access_token=' . $accessToken;
$feed = array();
while ($url) {
// Get the data from Facebook.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$data = json_decode(curl_exec($curl));
// Store the feed to $feed.
if (is_array($data->data)) $feed = array_merge($feed, $data->data);
// Load the next page or quit.
if (isset($data->paging->next)) $url = $data->paging->next;
else $url = false;
}
// Feed will contain the oldest feed items.
$feed = array_slice($feed, -$qtyRequired);
You might be able to speed it up by specifying the exact fields you need.

eBay API - FindItemsAdvanced call in PHP does not return any response

Im trying to perform a simple call using ebays search API, when I make a call I get no response, and the problem is with the actual call itself.
$endpoint = 'http://open.api.ebay.com/shopping?';
$responseEncoding = 'XML';
$version = '631'; // API version number
$appID = 'asdf3e6e3';
$itemType = "AllItemTypes";
$itemSort = "EndTime";
//find items advanced
$apicalla = "$endpoint"
."callname=FindItemsAdvanced"
."&version=$version"
."&siteid=0"
."&appid=$appID"
."&MaxEntries=10"
."&ItemSort=EndTime"
."&ItemType=AllItemTypes"
."&IncludeSelector=SearchDetails"
."&responseencoding=$responseEncoding";
$resp = simplexml_load_file($apicalla);
this call is the equivalent to
http://open.api.ebay.com/shopping?callname=FindItemsAdvanced&version=631&siteid=0&appid=asdf3e6e3&MaxEntries=10&ItemSort=EndTime&ItemType=AllItemTypes&IncludeSelector=SearchDetails&responseencoding=XML
My question is what am I missing to make this simple search call?
It looks like you're trying to use eBay's Shopping API, specifically the FindItemsAdvanced call which I believe was deprecated quite some time ago and may no longer be functional (I no longer see it in the call reference). What you want to do is use use findItemsAdvanced from eBay's Finding API.
First, you'll need to change your API endpoint & query string parameters a bit (see the aforementioned findItemsAdvanced call reference for the specifics, but I believe it'll look more like this (I haven't touched my findItemsAdvanced calls in at least 6 months, so I haven't tested this):
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1?';
$responseEncoding = 'XML';
$version = '1.8.0'; // API version number (they're actually up to 1.11.0 at this point
$appID = 'asdf3e6e3';
$itemSort = "EndTimeSoonest";
//find items advanced
$apicalla = "$endpoint"
."OPERATION-NAME=findItemsAdvanced"
."&SERVICE-VERSION=$version"
."&GLOBAL-ID=EBAY-US"
."&SECURITY-APPNAME=$appID"
//."&MaxEntries=10" // look for an equivalent for this (maybe paginationInput.entriesPerPage?)
."&sortOrder=EndTimeSoonest"
//."&ItemType=AllItemTypes" // not needed AFAICT, otherwise look at itemFilterType
."&descriptionSearch=true";
."& RESPONSE-DATA-FORMAT=$responseEncoding";
$resp = simplexml_load_file($apicalla);
In addition to this, to use findItemsAdvanced, you must specify what you're searching for either by category (categoryId) or by keywords (keywords), hence the "Please specify a query!" error message.
So, you also need to add something like the following (assuming keywords):
$keywords = "something";
$apicalla .= "&keywords=" . urlencode($keywords);
Giving you the following:
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1?';
$responseEncoding = 'XML';
$version = '1.8.0'; // API version number (they're actually up to 1.11.0 at this point
$appID = 'asdf3e6e3';
$itemSort = "EndTimeSoonest";
$keywords = "something"; // make sure this is a valid keyword or keywords
//find items advanced
$apicalla = "$endpoint"
."OPERATION-NAME=findItemsAdvanced"
."&SERVICE-VERSION=$version"
."&GLOBAL-ID=EBAY-US"
."&SECURITY-APPNAME=$appID"
//."&MaxEntries=10" // look for an equivalent for this (maybe paginationInput.entriesPerPage?)
."&sortOrder=$itemSort"
//."&ItemType=AllItemTypes" // not needed AFAICT, otherwise look at itemFilterType
."&descriptionSearch=true";
."& RESPONSE-DATA-FORMAT=$responseEncoding"
."&keywords=" . urlencode($keywords);
$resp = simplexml_load_file($apicalla);
One final note: If you want to load further details of specific items that you find in your results, you'll still want to use the Shopping API (specifically the GetSingleItem & GetMultipleItems calls). So, you may ultimately use a mix of the Shopping & Finding APIs.
It should be something like:
<?php
$url = 'http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsAdvanced&SERVICE-VERSION=1.11.0&SECURITY-APPNAME=YOUR_APP_ID&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&paginationInput.entriesPerPage=2&keywords=ipod&siteid=203&GLOBAL-ID=EBAY-IN';
$xml = file_get_contents( $url );
$xml = simplexml_load_string( $url );
?>
Log-in to your ebay developer account and click on this link: Test your calls with API Test Tool
Hope this helps.

Youtube PHP API: Verify a youtube username matches up with authentication token

I have a web application where I'm having the user log into youtube to authorize my web application to access their account. However, I need their youtube user name to actually list their uploaded videos, and I want to ensure that the youtube username they provide matches up with the account they used to authorize with. In other words, I only want the user to be able to share videos that they've uploaded, and not someone else's. Is there a way to do this?
I had done this before with C# .Net with the following code:
YouTubeRequestSettings yt_settings = new YouTubeRequestSettings(<devID name>, devKey, auth);
YouTubeRequest yt_request = new YouTubeRequest(yt_settings);
Uri uri = new Uri("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
Feed<Video> videoFeed = yt_request.Get<Video>(uri);
string uploader = "";
if (videoFeed.Entries.Count() > 0)
uploader = videoFeed.Entries.ElementAt(0).Uploader;
But when I try something similar with php I get what appears to be a standard feed:
$yt = new Zend_Gdata_YouTube($http_client,<devID name>,null,$_yt_dev_key);
$feed_url = urlencode("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
$videoFeed = $yt->getVideoFeed();
if(count($videoFeed) > 0)
{
$videoEntry = $videoFeed[0];
echo var_dump($videoEntry);
}
Anyone have any idea if I'm doing something wrong?
------------------ UPDATE ----------------------
I have the solution to getting youtube video feed for the user in the authenticated session. $videoFeed = $yt->getuserFeed("default");
Though, I'm still looking at how to get the uploader name from this so that I can perform further video listings directly from javascript (like I had done with my old C#/Asp .Net web app).
----------- A RATHER ROUGH SOLUTION ------------
Well, this isn't exactly an elegant solution, but it's what I have working.
The following will extract the youtube username from a VideoEntry object...
$videoFeed = $yt->getuserUploads("default");
if(count($videoFeed) > 0)
{
$videoEntry = $videoFeed[0];
$v_dump = var_export($videoEntry, true);
$check_for = "http://gdata.youtube.com/feeds/api/users/";
$pos = strpos($v_dump,$check_for);
$start_pos = $pos + strlen($check_for);
$user_name = "";
for($i = $start_pos; $i < strlen($v_dump); $i++)
{
if(($v_dump[$i] == '/') or ($v_dump[$i] == '?'))
break;
$user_name .= $v_dump[$i];
}
echo $user_name;
}
Basically, I'm parsing through the entire video entry variable string representation for the first generic part of the feed url, then getting the next token in the url which is the username. In other words, I'm locating this url in the var dump and parsing the username out:
http://gdata.youtube.com/feeds/api/users/<username>/uploads
If anyone can find a better and cleaner way to do it, that'd be awesome. Parsing a large string like this seems to be a very dirty way of doing this.
See http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Enabling_user_interaction
It says,
Note: Using the string default instead of a username retrieves the profile of the currently authenticated user.
So you can get all the user data about the currently authenticated user using this.
Hope it helps!
Try this:
$user = $yt->getUserProfile('default');
$username = $user->getUserName();

How do I insert into non-default Google Calendar using Zend / PHP

I'm trying to insert a new event into Google Calendar, which I can do fine by the following:
$sname = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($userName,$password,$sname);
$service = new Zend_Gdata_Calendar($client);
$event = $service->newEventEntry();
$event->title = $service->newTitle($name);
$event->when = array($when);
$newEvent = $service->insertEvent($event);
However, this will only insert into the default calendar. I am trying to use the URI argument of insertEvent to get where I need to go:
$uri = "http://www.google.com/calendar/feeds/default/something#somethingelse.com/private/full";
$newEvent = $service->insertEvent($event, $uri);
This hasn't worked. I've tried encoding and decoding the URI, which gives me different error messages, but I just can't get this to work. I've been Googling solutions for hours now with no luck. Can anyone help?
Thanks.
- Dave
Found the issue:
The URI above doesn't work:
"http://www.google.com/calendar/feeds/default/something#somethingelse.com/private/full";
should not have the default section there.
Also, the # sign should be replaced by encoding. So it should be:
"http://www.google.com/calendar/feeds/something%40somethingelse.com/private/full";
I have been fighting this for awhile and I finally got it to work. This is not wrapped nicely, but this is the answer I was looking for
First you have to get the calendar ID. If you go into your calendar and click on the down arrow icon next to the name you will see an option for Calendar Settings. Select that menu item. Near the bottom you will see Calendar Address. It will take a moment to find the Calendar ID and it will look something like this
(Calendar ID: o4g3921hdiaq5p8kdat2l4vgis#group.calendar.google.com)
You want o4g39j1hdihq4p8kdat2l4vgis#group.calendar.google.com
Down the road I will want to get this in php but for now I can hard code. So I set a variable to this
$calendar_user = 'o4g39j1hdihq4p8kdat2l4vgis#group.calendar.google.com';
When retrieving events you will setUser like this
$gdataCal = new Zend_Gdata_Calendar($client);
$query = $gdataCal->newEventQuery();
$query->setUser($calendar_user);
But when you are adding event you can't do that you have to use the $calendar_user at the point where you insert the event.
So here is the complete code
$calendar_user = 'o4g39j1hdihq4p8kdat2l4vgis#group.calendar.google.com';
$user = 'myemail#yahoo.com';
$pass = 'mygooglecalendarpassword';
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service);
$gc = new Zend_Gdata_Calendar($client);
$newEntry = $gc->newEventEntry();
$newEntry->title = $gc->newTitle("mytitle");
$newEntry->where = array($gc->newWhere("meeting place"));
$newEntry->content = $gc->newContent("Meeting description");
$newEntry->content->type = 'text';
$when = $gc->newWhen();
$when->startTime = "{'2012-06-28'}T{'10:00'}:00.000{'-08'}:00";
$when->endTime = "{'2012-06-28'}T{'11:00'}:00.000{'-08'}:00";
$newEntry->when = array($when);
$createdEvent = $gc->insertEvent($newEntry, "http://www.google.com/calendar/feeds/".$calendar_user."/private/full");
Now I hope that is helpful. I tell you it took me way too long to figure this out, but I am not the smartest guy in the world.

Categories