Support

Home Forums Event Espresso Premium example php for JSON needed

example php for JSON needed

Posted: December 29, 2013 at 5:32 am

Viewing 9 reply threads


Tristan Mills

December 29, 2013 at 5:32 am

I need to write a PHP page to show a list of attendees to an event using the json api, is there any example code i could look at to get started? I have looked at the php examples in the json documentation but they are for ‘public’ – I assume I need to write something which will authenticate and use the session id to get some results. Any examples or pointers would be much appreciated ๐Ÿ™‚


Tristan Mills

December 29, 2013 at 8:51 am

I am trying to use php curl function but cannot authenticate, although if I paste in a browser it works fine and provides a session id..

code I am using is:

<?php 

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://mydomain.com/espresso-api/v1/authenticate");

// Puts return in variable rather than the browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");

// grab URL and pass it to the variable
$jsonStr = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

echo $jsonStr;
?>

which gives:
{“status”:”Bad username and password combination.”,”status_code”:401}

if i paste:
http://mydomain.com/espresso-api/v1/authenticate?username=username&password=password it works, ie I get
{“status”:”OK”,”status_code”:200,”body”:{“session_key”:”q4cpdlxoey”}}

what am I doing wrong here?


Sidney Harrell

December 31, 2013 at 5:32 pm

I don’t think you should be using CURLOPT_USERPWD. Rather, put the username and password into the URL, just like you did when putting it into the browser.


Tristan Mills

January 1, 2014 at 7:23 am

thanks, that works fine now, here’s my code to grab the session key in case it helps anyone

<?php 

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://mydomain.com/espresso-api/v1/authenticate?username=myusername&password=mypassword");

// Puts return in variable rather than the browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// grab URL and pass it to the variable
$jsonStr = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

// decode json and put in array
$data = json_decode($jsonStr, true);

// grab the session key out of the multidimensional array to use later
$sesskey = $data['body']['session_key'];

echo "session key ".$sesskey;

?>


Tristan Mills

January 2, 2014 at 9:29 am

i have code working now to query /registrations , e.g.
<pre class=”brush: html; gutter: true; first-line: 1; highlight: []; html-script: false”>http://mydomain.com/espresso-api/v1/registrations/&quot;.$sesskey."?name__like=%25". $_POST['location'] . "%25&Datetime.event_start__e=" . ($_POST['date']) . "%2019:30:00"
– this is a bit messy as i had to add a start time on the end of that to get results, thats another issue; i can make sure i am getting data by using this to return everything
<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”>array_walk_recursive($data, function($value, $key){echo "$key=$value" . PHP_EOL;});

I have some test registrations in the database. I can count the number of registrations like this:
<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”>$count = count($data['body']['Registrations']);
– i get an expected number
but what i am trying to do is list each registrant for a particular event and date, so in the course of testing, if I do
<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”>$count = count($data['body']['Registrations']['Attendee']['firstname']); – then I get a 0 count, when I would expect to get 9 first names, which I could in theory echo with my foreach loop. Looks like I’m approaching this wrong again, any ideas?

If I could also request again if anyone has any example code for JSON to list the attendees to an event it would save me several weeks working it out probably ๐Ÿ™‚


Tristan Mills

January 2, 2014 at 9:32 am

ps – obviously i can’t get the hang of the code highlighting here.. although I did try


Tristan Mills

January 7, 2014 at 6:35 am

ok, maybe you could help me with this? having made some progress [I’m happy to share the finished code to add to the json docs btw – when it is finished] – kind of a php question, but im sure you guys will know why this is wrong easily..

this is my foreach code to get the first and last names for each attendee:

foreach ($the_info_array[‘body’][‘Registrations’] as $registrations)
{

foreach ($registrations as $regvalue){
echo $regvalue[‘firstname’].” “.$regvalue[‘lastname’];
}
echo “<br>”;
}

this prints:

a a2 2 5 5 dave davey
a a2 2 5 5 dave davey
a a2 2 5 5 dave davey
a a2 2 5 5 dave davis
a a2 2 5 5 dave davis
a a2 2 5 5 dave davis

rather than what i want ie

dave davey
dave davey
dave davey
dave davis
dave davis
dave davis

what are the extra characters?


Tristan Mills

January 22, 2014 at 5:25 am

nearly finished this code, just have to filter out pending payments from my results. Why doesn’t adding this to the end of my json query string do it?

&Transaction.status__like=%25complete%25


Tristan Mills

January 25, 2014 at 8:16 am

[code language=”PHP”]
<?php $test=1;
?>
[/code]


Tristan Mills

January 25, 2014 at 8:17 am

<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”><?php $test=1;
?>


Tristan Mills

January 25, 2014 at 8:24 am

<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”><?php

// SET main domain name to use
$url_domain = "http://domain.com&quot;;
?>


Tristan Mills

January 25, 2014 at 8:25 am

can anyone explain how to post some code like its been formatted above “paste your code in the Text editor, highlight it, then click the language below to wrap your code in tags” – doesnt seem to work

Viewing 9 reply threads

The support post ‘example php for JSON needed’ 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