Facebook Graph API Paging in Batches - php

I am struggling to understand how to make use of the paging mechanism with batch requests to Facebook's Graph API.
My issue with the code below is that despite attempting to track an offset for each facebook_id that is in my database, I'm unable to determine when a particular request has come up empty and therefore shouldn't be requested again.
I'm parsing the facebook_id's from the paging array in the responses. At the point when no more posts are available, FB returns an empty data array and therefore I'm unable to recognize which request has no more pages, and thus reduce the subsequent requests. Each time I'm querying FB with $fb_batch_limit urls despite the fact not all requests have returned a paging token from the previous call.
How would a real programmer tackle this problem?
$posts = array();
//$fb_batch_limit = 30;
$fb_batch_limit = 2;
$fb_req_limit=10;
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_app_secret
));
$facebook->setAccessToken($fb_user_token);
$index = 0;
$since = '1Jan14';
$until = 'today';
$num_records_res = $oDB->select("SELECT count(*) as count FROM facebook f");
$num_records_row = mysqli_fetch_array($num_records_res);
$num_records = $num_records_row['count'];
while($index < $num_records) {
printf("# %d to %d\n", $index, $index+$fb_batch_limit);
$res = $oDB->select("SELECT facebook_id FROM facebook f ".
"LIMIT $index, $fb_batch_limit");
$index = $index + $fb_batch_limit;
$offsets = array();
while($rr = mysqli_fetch_array($res)){
$offsets[$rr['facebook_id']] = 0;
}
if (!count($offsets)) {
printf("no more accounts\n");
break;
}
$r=1;
do {
$urls = array();
foreach ($offsets as $fbid => $offset) {
$tmp["method"] = 'GET';
$tmp["relative_url"] = "/".$fbid."/feed?fields=likes.limit(1).summary(true)&offset=$offset&limit=$fb_req_limit";//&since=$since&until=$until";
$tmp["include_headers"] = false;
$urls[] = $tmp;
}
$numurls = count($urls);
printf("$r: fetching %d urls have %d posts\n", $numurls, count($posts));
$response = $facebook->batch($urls);
foreach ($response as $result) {
if (array_key_exists("error", $result)) {
//FIXME handle rate limits somehow
print_r($result);
die;
}
if (array_key_exists("paging", $result)) {
$fbid = substr(parse_url($result['paging']['previous'])['path'],6);
$fbid = substr($fbid, 0, -5);
if (($count = count($result['data'])) > 0) {
$posts = array_merge($posts, $result['data']);
if (array_key_exists("paging", $result) && array_key_exists("next", $result["paging"])) {
if (array_key_exists($fbid, $offsets)) {
$offsets[$fbid] += $count;
}
}
}
else {
printf("$fbid complete with ".
$offsets[$fbid]." posts\n");
unset($offsets[$fbid]);
}
}
else {
// how to stop requesting this single url?
print_r($result);
echo "--\n";
$numurls--;
}
}
} while (++$r && $numurls);
}

Related

PHP - Calculate Average Rating Not Working?

I've been working on a movie review website and I can't quite get the calculate average rating functionality working. In the code below, In the first instance I am grabbing the current rating of the movie [$totalRating] via an API and setting the counter [$numRatings]. Then via an API I am collecting all of the ratings of that movie, attempting to to add them to the total rating, updating the counter within the loop.
However, when I rate movies they simply are updated to the registered rating (say I input an 8 it becomes an 8) and the division never takes place. Was wondering if anyone has any solutions or can see where I am going wrong?
function setUserRating($movieID)
{
$ep = "http://localhost:8888/MovieRating/api/?movieDetails=$movieID";
$response = file_get_contents($ep);
$Movies = json_decode($response, true);
$baseRating = $Movies[0]['movieRating'];
$numRatings = 1;
$totalRating = $baseRating;
$ep = "http://localhost:8888/MovieRating/api/?allMovieRatings=$movieID";
$resp = file_get_contents($ep);
$userRatings = json_decode($resp, true);
if (isset($userRatings['movie_ratings'])) {
$reviews = $userRatings['movie_ratings'];
}
if (isset($reviews)) {
foreach ($reviews as $row) {
$numRatings++;
$totalRating = $totalRating + $row['rating'];
}
}
$avgUserRating = $totalRating / $numRatings;
return $avgUserRating;
}
Might just need a small adjustment
function setUserRating($movieID)
{
$ep = "http://localhost:8888/MovieRating/api/?movieDetails=$movieID";
$response = file_get_contents($ep);
$Movies = json_decode($response, true);
$baseRating = $Movies[0]['movieRating'];
$numRatings = 0; //1
$totalRating = 0; //$baseRating;
$ep = "http://localhost:8888/MovieRating/api/?allMovieRatings=$movieID";
$resp = file_get_contents($ep);
$userRatings = json_decode($resp, true);
if (array_key_exists($userRatings['movie_ratings'])) {
$reviews = $userRatings['movie_ratings'];
}
if (isset($reviews)) {
foreach ($reviews as $row) {
++$numRatings;
$totalRating += $row['rating'];
}
}
return $numRatings > 0 ? $totalRating / $numRatings : $baseRating;
}

Sphinx Query is returning null results in PHP

I am currently using PHP to query from my sphinx index. The index is building properly, however the search is not.
Originally I had my query set up like the following:
private function _doMapSearchAll($textSearch, $typeFilterArr, $rads = array(), $centre = array(), $showFavourites = false) {
// lookup the location based on the search and return the results in that
// map bounds ignore for boundary searches
$rsArr = array();
$skipDataSearch = false;
if (!COUNT($rads)) {
// handle airport code lookups if we have 3 characters
if (strlen($textSearch) === 3) {
$rsArr = $this->_doMapAirportSearch($textSearch);
if (COUNT($rsArr)) {
$skipDataSearch = true;
}
}
// still no results based on the above search, do generic map search
if ($skipDataSearch === false) {
$rsArr = $this->_doMapGeoSearch($textSearch, $typeFilterArr, $centre);
if (COUNT($rsArr['results']) > 0) {
$skipDataSearch = false;
}
}
}
// if we are doing a boundary search, or we have no results from the above,
// get all results based on our location
if ($skipDataSearch === false) {
// fall back onto searching via the data
// normalise the search string
$originalSearchString = $this->doNormalisation($textSearch);
// use sphinx for the search
$sphinx = $this->_getSphinxConnection();
$sphinx->setLimits(0, $this->container->getParameter('search_max_results'), $this->container->getParameter('search_max_results'));
if (COUNT($typeFilterArr)) {
$sphinx->SetFilter('fldSiteTypeUID_attr', $typeFilterArr);
}
// if we're doing a boundary search, skip the search string
if (COUNT($rads) > 0) {
$originalSearchString = '';
if ($showFavourites === false) {
//$sphinx->SetFilterFloatRange('lat_radians', min($rads['minLat'], $rads['maxLat']), max($rads['minLat'], $rads['maxLat']));
//$sphinx->SetFilterFloatRange('long_radians', min($rads['minLon'], $rads['maxLon']), max($rads['minLon'], $rads['maxLon']));
if ($rads['minLat'] > $rads['maxLat']) {
$rads['minLat'] = $rads['minLat'] - 360;
}
if ($rads['minLon'] > $rads['maxLon']) {
$rads['minLon'] = $rads['minLon'] - 180;
}
$sphinx->SetFilterFloatRange('lat_radians', $rads['minLat'], $rads['maxLat']);
$sphinx->SetFilterFloatRange('long_radians', $rads['minLon'], $rads['maxLon']);
}
}
// order by centre point
if (COUNT($centre) > 0) {
// otherwise start in the centre
$sphinx->SetGeoAnchor('lat_radians', 'long_radians', (float) $centre['centreLat'], (float) $centre['centreLon']);
$lintDistanceLimit = 999999999.0; // everything
if ($showFavourites === false && isset($rads['maxLat'])) {
$radiusMiles = $this->get('geolocation_helper')->getSeparation(
rad2deg($rads['maxLat']),
rad2deg($rads['minLon']),
rad2deg($rads['minLat']),
rad2deg($rads['maxLon']),
"M"
);
$lintDistanceLimit = $radiusMiles * 1609; // miles to meters...
}
$sphinx->SetFilterFloatRange('geodist', 0.0, (float)$lintDistanceLimit);
$sphinx->SetSortMode(SPH_SORT_EXTENDED, 'geodist ASC');
} else {
// apply search weights
$sphinx->SetFieldWeights(array('fldTown_str' => 100, 'fldAddress1_str' => 30, 'fldSiteName_str' => 20));
}
// if we should be limiting to only favourites, pickup the selected
// favourites from cookies
if ($showFavourites === true) {
$request = $this->container->get('request_stack')->getCurrentRequest();
$favourites = explode(',', $request->cookies->get('HSF_favlist'));
if (count($favourites)) {
foreach ($favourites as $k => $favourite) {
$favourites[$k] = (int)$favourite;
}
$sphinx->SetFilter('fldUID_attr', $favourites);
}
}
$rs = $sphinx->Query($originalSearchString, $this->container->getParameter('sphinx_index'));
echo json_encode($sphinx);
$rsArr['results'] = array();
if (isset($rs['matches']) && count($rs['matches'])) {
// update the search text to what we actually searched for, this
// is needed in case we've updated/set $rsArr['searchText']
// after a geolookup
$rsArr['searchText'] = $textSearch;
// clear any previous bounds set by the geolookup, we don't want this
// if we have direct text match results
$rsArr['geobounds'] = array();
if (isset($rs['matches']) && count($rs['matches'])) {
foreach ($rs['matches'] as $k => $match) {
$rsArr['results'][$k] = $this->_remapSphinxData($k, $match);
}
}
// sort the results by distance
usort($rsArr['results'], function ($a, $b) {
return $a['fldDistance'] - $b['fldDistance'];
});
}
}
// add on the total record count
$rsArr['total'] = (int) COUNT($rsArr['results']);
return $rsArr;
}
That was returning nothing and giving me the error for GEODIST():
{"_host":"sphinx","_port":36307,"_path":"","_socket":false,"_offset":0,"_limit":250,"_mode":0,"_weights":[],"_sort":4,"_sortby":"geodist ASC","_min_id":0,"_max_id":0,"_filters":[{"type":2,"attr":"geodist","exclude":false,"min":0,"max":999999999}],"_groupby":"","_groupfunc":0,"_groupsort":"#group desc","_groupdistinct":"","_maxmatches":"250","_cutoff":0,"_retrycount":0,"_retrydelay":0,"_anchor":{"attrlat":"lat_radians","attrlong":"long_radians","lat":0.9300859583877783,"long":-2.0943951023931953},"_indexweights":[],"_ranker":0,"_rankexpr":"","_maxquerytime":0,"_fieldweights":[],"_overrides":[],"_select":"*","_error":"searchd error: geoanchor is deprecated (and slow); use GEODIST() expression","_warning":"","_connerror":false,"_reqs":[],"_mbenc":"","_arrayresult":false,"_timeout":0}{"results":[],"bounds":{"bllat":53.395603,"trlat":53.7159857,"bllng":-113.7138017,"trlng":-113.2716433},"geocentre":{"lat":53.5461245,"lon":-113.4938229},"checksum":"204a43923452936b00a10c8e566c4a48d4fdb280f97fd4042646eb45c8257bbc","searchText":"Edmonton, AB, Canada","skipResults":true,"showFavourites":false}
To fix this I changed the SetGeoAnchor function to the following:
private function _doMapSearchAll($textSearch, $typeFilterArr, $rads = array(), $centre = array(), $showFavourites = false) {
// lookup the location based on the search and return the results in that
// map bounds ignore for boundary searches
$rsArr = array();
$skipDataSearch = false;
if (!COUNT($rads)) {
// handle airport code lookups if we have 3 characters
if (strlen($textSearch) === 3) {
$rsArr = $this->_doMapAirportSearch($textSearch);
if (COUNT($rsArr)) {
$skipDataSearch = true;
}
}
// still no results based on the above search, do generic map search
if ($skipDataSearch === false) {
$rsArr = $this->_doMapGeoSearch($textSearch, $typeFilterArr, $centre);
if (COUNT($rsArr['results']) > 0) {
$skipDataSearch = false;
}
}
}
// if we are doing a boundary search, or we have no results from the above,
// get all results based on our location
if ($skipDataSearch === false) {
// fall back onto searching via the data
// normalise the search string
$originalSearchString = $this->doNormalisation($textSearch);
// use sphinx for the search
$sphinx = $this->_getSphinxConnection();
$sphinx->setLimits(0, $this->container->getParameter('search_max_results'), $this->container->getParameter('search_max_results'));
if (COUNT($typeFilterArr)) {
$sphinx->SetFilter('fldSiteTypeUID_attr', $typeFilterArr);
}
// if we're doing a boundary search, skip the search string
if (COUNT($rads) > 0) {
$originalSearchString = '';
if ($showFavourites === false) {
//$sphinx->SetFilterFloatRange('lat_radians', min($rads['minLat'], $rads['maxLat']), max($rads['minLat'], $rads['maxLat']));
//$sphinx->SetFilterFloatRange('long_radians', min($rads['minLon'], $rads['maxLon']), max($rads['minLon'], $rads['maxLon']));
if ($rads['minLat'] > $rads['maxLat']) {
$rads['minLat'] = $rads['minLat'] - 360;
}
if ($rads['minLon'] > $rads['maxLon']) {
$rads['minLon'] = $rads['minLon'] - 180;
}
$sphinx->SetFilterFloatRange('lat_radians', $rads['minLat'], $rads['maxLat']);
$sphinx->SetFilterFloatRange('long_radians', $rads['minLon'], $rads['maxLon']);
}
}
// order by centre point
if (COUNT($centre) > 0) {
// otherwise start in the centre
// $sphinx->SetGeoAnchor('lat_radians', 'long_radians', (float) $centre['centreLat'], (float) $centre['centreLon']);
$centreLat = (float) $centre['centreLat'];
$centreLon = (float) $centre['centreLon'];
$sphinx->SetSelect("GEODIST(lat_radians, long_radians, $centreLat, $centreLon) AS geodist");
$lintDistanceLimit = 999999999.0; // everything
if ($showFavourites === false && isset($rads['maxLat'])) {
$radiusMiles = $this->get('geolocation_helper')->getSeparation(
rad2deg($rads['maxLat']),
rad2deg($rads['minLon']),
rad2deg($rads['minLat']),
rad2deg($rads['maxLon']),
"M"
);
$lintDistanceLimit = $radiusMiles * 1609; // miles to meters...
}
$sphinx->SetFilterFloatRange('geodist', 0.0, (float)$lintDistanceLimit);
$sphinx->SetSortMode(SPH_SORT_EXTENDED, 'geodist ASC');
} else {
// apply search weights
$sphinx->SetFieldWeights(array('fldTown_str' => 100, 'fldAddress1_str' => 30, 'fldSiteName_str' => 20));
}
// if we should be limiting to only favourites, pickup the selected
// favourites from cookies
if ($showFavourites === true) {
$request = $this->container->get('request_stack')->getCurrentRequest();
$favourites = explode(',', $request->cookies->get('HSF_favlist'));
if (count($favourites)) {
foreach ($favourites as $k => $favourite) {
$favourites[$k] = (int)$favourite;
}
$sphinx->SetFilter('fldUID_attr', $favourites);
}
}
$rs = $sphinx->Query($originalSearchString, $this->container->getParameter('sphinx_index'));
echo json_encode($sphinx);
$rsArr['results'] = array();
if (isset($rs['matches']) && count($rs['matches'])) {
// update the search text to what we actually searched for, this
// is needed in case we've updated/set $rsArr['searchText']
// after a geolookup
$rsArr['searchText'] = $textSearch;
// clear any previous bounds set by the geolookup, we don't want this
// if we have direct text match results
$rsArr['geobounds'] = array();
if (isset($rs['matches']) && count($rs['matches'])) {
foreach ($rs['matches'] as $k => $match) {
$rsArr['results'][$k] = $this->_remapSphinxData($k, $match);
}
}
// sort the results by distance
usort($rsArr['results'], function ($a, $b) {
return $a['fldDistance'] - $b['fldDistance'];
});
}
}
// add on the total record count
$rsArr['total'] = (int) COUNT($rsArr['results']);
return $rsArr;
}
This gives me back results however they look like this:
{"_host":"sphinx","_port":36307,"_path":"","_socket":false,"_offset":0,"_limit":250,"_mode":0,"_weights":[],"_sort":4,"_sortby":"geodist ASC","_min_id":0,"_max_id":0,"_filters":[{"type":2,"attr":"geodist","exclude":false,"min":0,"max":999999999}],"_groupby":"","_groupfunc":0,"_groupsort":"#group desc","_groupdistinct":"","_maxmatches":"250","_cutoff":0,"_retrycount":0,"_retrydelay":0,"_anchor":[],"_indexweights":[],"_ranker":0,"_rankexpr":"","_maxquerytime":0,"_fieldweights":[],"_overrides":[],"_select":"GEODIST(lat_radians, long_radians, 0.93008595838778, -2.0943951023932) AS geodist","_error":"","_warning":"","_connerror":false,"_reqs":[],"_mbenc":"","_arrayresult":false,"_timeout":0}{"results":[[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,34362776,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,38279990,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,7963188,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,7971966,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,31790051,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,7972301,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,33589292,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,33589642,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,7962913,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,31789941,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,7962178,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,49484181,null,""],[null,null,null,null,null,null,null,"Other","http:\/\/localhost:8080\/themes\/docker\/images\/Site-Type-Other.png",null,31795436,null,""],
.....
My searchd config looks like this:
searchd
{
listen = 36307
listen = 9306:mysql41
log = /opt/sphinx/searchd.log
query_log = /opt/sphinx/query.log
read_timeout = 5
max_children = 30
pid_file = /opt/sphinx/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
binlog_path = /opt/sphinx/
}
I am new tp Sphinx and need help correcting this query. Any thoughts?
Edit: This is sphinx version 3.4.1
So after hours on this over the past few days, I have found the answer...:
I was missing the * in the statement:
$sphinx->SetSelect("*, GEODIST($centreLat, $centreLon, lat_radians, long_radians) as geodist");
I hope this helps anyone who has this issue in the future

Catch Tweets with JSON and sort by likes?

I am currently running a wordpress backend and want to display some tweets based on hastags on my website. For the general API request and database storage, I use this function:
private function parseRequest($json) {
$tmp = $json;
$result = array();
if (isset($json['statuses'])) {
$tmp = $json['statuses'];
}
if (isset($tmp) && is_array($tmp)){
foreach ($tmp as $t) {
$this->image = null;
$this->media = null;
$tc = new \stdClass();
$tc->feed_id = $this->id();
$tc->id = $t['id_str'];
$tc->type = $this->getType();
$tc->nickname = '#'.$t['user']['screen_name'];
$tc->screenname = (string)$t['user']['name'];
$tc->userpic = str_replace('.jpg', '_200x200.jpg', str_replace('_normal', '', (string)$t['user']['profile_image_url']));
$tc->system_timestamp = strtotime($t['created_at']);
$tc->text = $this->getText($t);
$tc->userlink = 'https://twitter.com/'.$t['user']['screen_name'];
$tc->permalink = $tc->userlink . '/status/' . $tc->id;
$tc->media = $this->getMedia($t);
#$tc->additional = array('shares' => (string)$t['retweet_count'], 'likes' => (string)$t['favorite_count'], 'comments' => (string)$t['reply_count']);
if ($this->isSuitablePost($tc)) $result[$tc->id] = $tc;
}
}
return $result;
}
Now I am looking for a function that counts all the variable in the "additional array together e.g. shares + likes + comments and sorts all posts based on the resulting number.
I am using the standard wordpress sql database. I cannot find a solution or I am just blind.
Thanks in regards
You could use a simple usort function:
usort($tc, function($a, $b) {
$a_sum = array_sum($a->additional);
$b_sum = array_sum($b->additional);
if ($a_sum == $b_sum) {
return 0;
}
return ($a_sum < $b_sum) ? -1 : 1;
});

Google Analytics PHP API (GAPI) non-mobile browsers

I am trying to display the top 5 of non-mobile browsers with GAPI.
I can't seem to find a way of doing this, is this even possible?
This is how I get the percentage of mobile visits:
$ga->requestReportData(GA_PROFILE_ID, array("isMobile"), array("visits"), '-visits', null, $startdate, $enddate);
foreach ($ga->getResults() AS $result) {
if ((string)$result == "Yes") $mobile["yes"] = $result->getVisits();
else $mobile["no"] = $result->getVisits();
}
This is how I ended up doing it:
public function getDevices($max = 5) {
$this->ga->requestReportData($this->profileid, array('mobileDeviceInfo'), array('visits'), '-visits', 'mobileDeviceInfo != (not set) && mobileDeviceInfo != (not provided)', $this->startdate, $this->enddate, 1, $max);
foreach ($this->ga->getResults() as $result) {
$device[] = (string)$result;
$visits[] = $result->getVisits();
}
if (!isset($device) OR !isset($visits)) {
$device[] = "Niet genoeg data beschikbaar";
$visits[] = "0";
}
$return = array("device" => $device, "visits" => $visits);
return $return;
}

Flickr API returns inconsistent number of photos when using multiple tags

I'm trying to use my Flickr account as a 'host' for an image gallery. I've tagged 251 photos with a common tag 'golftournament' and each one with the year and any players in the photo.
So, for example, three random photos may have the following tags:
golftournament dan steve 2005 (dan and steve in this photo from 2006)
golftournament 2006 (no players in this photo from 2006)
golftournament 2008 paul dan (paul and dan in this photo from 2008)
When I make an API call, it returns an inconsistent total number of photos if I set the 'tags' part of the API call to tags=golftournament,dan,2005 and the tag_mode to tagmode=all.
Sometimes I get 13 photos in the result, sometimes I get 12 photos and sometimes I actually get the correct number of photos (14)
I'm using a PHP library, but that's irrelevant because I see the same results in the Flickr API Explorer: http://www.flickr.com/services/api/explore/?method=flickr.photos.search)
Is there any reason why the Flickr API is so inconsistent in this regard?
Cheers,
Dan
Just to update on this, in the end I simply got all photos from a photoset and then built two arrays of years and players from the tags. Finally, I used what the request was to return the correct photographs. I'm using the excellent phpFlickr library and here's my entire API script:
<?php
// Initialise Flickr API library and authentication
require_once("phpFlickr.php");
$f = new phpFlickr('<api key>', '<secret>');
$f->setToken('<token>');
// Get all photos from the photoset
$page = 0;
$perpage = 500;
$all = array('photos' => array(), 'total' => 0);
while ($perpage * $page < $all['total'] || $all['total'] == 0) {
$p = $f->photosets_getPhotos('<photoset number>', 'tags', NULL, $perpage, $page + 1);
$all['total'] = (integer) $p['photoset']['total'];
$all['photos'] += $p['photoset']['photo'];
$page++;
}
// Get all the available years/players from the tags
$years = array();
$players = array();
foreach ($all['photos'] as $key => $photo) {
$photo_tags = explode(' ', $photo['tags']);
$all['photos'][$key]['tags'] = $photo_tags;
foreach ($photo_tags as $tag) {
if (preg_match('/^[0-9]{4}$/', $tag)) {
if (!in_array($tag, $years)) {
$years[] = $tag;
}
} else {
if (!in_array($tag, $players)) {
$players[] = $tag;
}
}
}
}
rsort($years);
sort($players);
// Set year/player tags if set
$tags = array();
if (isset($_GET['year']) && in_array($_GET['year'], $years)) {
$tags['year'] = $_GET['year'];
}
if (isset($_GET['player']) && in_array($_GET['player'], $players)) {
$tags['player'] = $_GET['player'];
}
// Build output array and filter by year/person
$output = array(
'years' => $years,
'players' => $players,
'photos' => array()
);
foreach ($all['photos'] as $key => $photo) {
if (!isset($tags['year']) || in_array($tags['year'], $photo['tags'])) {
if (!isset($tags['player']) || in_array($tags['player'], $photo['tags'])) {
$output['photos'][] = array(
'thumbnail' => $f->buildPhotoURL($photo, 'Square'),
'image' => $f->buildPhotoURL($photo, 'Large'),
'tags' => implode(' ', $photo['tags'])
);
}
}
}
$output['totals']['total'] = count($output['photos']);
// Calculate paging
$output['perpages'] = array(32, 64, 128, 256);
if (isset($_GET['perpage']) && in_array($_GET['perpage'], $output['perpages'])) {
$output['perpage'] = $_GET['perpage'];
} else {
$output['perpage'] = $output['perpages'][0];
}
if ($output['totals']['total'] <= $output['perpage']) {
$output['pages'] = 1;
} else {
$output['pages'] = ceil($output['totals']['total'] / $output['perpage']);
}
if (isset($_GET['page']) && intval($_GET['page']) > 0 && intval($_GET['page']) <= $output['pages']) {
$page = $_GET['page'];
} else {
$page = 1;
}
$output['totals']['recordstart'] = (($page == 1) ? 0 : (($page - 1) * $output['perpage']));
$output['totals']['recordend'] = ($output['totals']['recordstart'] + $output['perpage']) - 1;
if ($output['totals']['recordend'] > ($output['totals']['total'] - 1)) {
$output['totals']['recordend'] = $output['totals']['total'] - 1;
}
$photos = array();
foreach ($output['photos'] as $index => $photo) {
if ($index >= $output['totals']['recordstart'] && $index <= $output['totals']['recordend']) {
$photos[] = $photo;
}
}
unset($output['photos']);
$output['photos'] = $photos;
ob_start("ob_gzhandler");
echo json_encode($output);
Hope someone finds this useful!
Dan
Apparently the flickr search functionality is bugged at the moment and flickr is working on a fix.

Categories