Support

Home Forums Event Espresso Premium JSON API Add-on Get Events within a Date Range

JSON API Add-on Get Events within a Date Range

Posted: December 12, 2015 at 2:27 pm

Viewing 3 reply threads


Doug Staneart

December 12, 2015 at 2:27 pm

I have over 200 events in my Espresso app, and I’m using the JSON API add-on to pull events to a second website. Since the automatic limit is 50 events and many of my events are recurring, the ID numbers are not in a series. So, my API is only pulling the first 50 events that have the lowest ID numbers, not the events with the earliest start dates. (See http://www.leadersinstitute.com/public-speaking/public-speaking-class.) Since I set the Houston classes up before the list in other cities, all 12 of the Houston events have lower ID numbers than events that I inputed later. So, the API is pulling Houston events that occur a year from now instead of events in other cities that will be next month. Here is the code:

function f_espresso_event_list( $atts ){
$curdate = date(“Y-m-d H:i:s”);
$ch = curl_init();
$url = “http://www.fearlesspresentations.com/espresso-api/v1/authenticate/”;
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$args = array(‘username’=>’XXXXXXXX’, ‘password’ => ‘XXXXXXXX’);
if(!empty($args) ) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$args);
}
$result = curl_exec($ch);
curl_close($ch);
$token = json_decode($result, true);
if(isset($token[‘status’]) && $token[‘status’] == ‘OK’){
$handle = curl_init();
$args = ‘?Datetime.event_start__gt=’.urlencode($curdate).”;
$url1 = “http://www.fearlesspresentations.com/espresso-api/v1/events/”.$token[‘body’][‘session_key’].”.json/”.$args;

//$url1 = “http://www.fearlesspresentations.com/espresso-api/v1/events/”.$token[‘body’][‘session_key’].”.json/”;
curl_setopt($handle, CURLOPT_URL, $url1);
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
/*$args = array(‘Datetime.event_start__gt’=>urlencode($curdate), ‘limit’ => 2);
if(!empty($args) ) {
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS,$args);
}*/
$result1 = curl_exec($handle);
curl_close($handle);
$data = json_decode($result1, true);
$html = ”;

function sortByOrder($a, $b) {
return strtotime($a[‘Datetimes’][0][‘event_start’]) – strtotime($b[‘Datetimes’][0][‘event_start’]);
}

Any idea on how to make the API pull events that are first in the date series versus lowest ID number?


Josh

  • Support Staff

December 14, 2015 at 3:54 pm

Hi Doug,

You can add an AND to the query, set a $futuredate variable one or two months from now (you’ll know about how far out), so your query looks like this:

$args = '?Datetime.event_start__gt='.urlencode($curdate).'&Datetime.event_start__lt='.urlencode($futuredate).'/';


Doug Staneart

December 14, 2015 at 8:49 pm

Josh, thanks, but I’m a little over my head on this (not a programmer). I tried to setup the variable for 3 months out using…

$futuredate = date(“Y-m-d H:i:s”,strtotime(“+3 months”, strtotime($futuredate)));

But now I’m getting nothing. I’m sure it is my syntax, but not sure what I’ve done wrong.


Josh

  • Support Staff

December 15, 2015 at 8:16 am

Hi Doug,

You’ll find some syntax examples in this entry at PHP.net:

http://php.net/manual/en/function.strtotime.php#example-2541

Viewing 3 reply threads

The support post ‘JSON API Add-on Get Events within a Date Range’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso