Support

Home Forums Event Espresso Premium Extra Characters in JSON API result set, using PHP.

Extra Characters in JSON API result set, using PHP.

Posted: May 12, 2014 at 4:40 pm


Mike Heath

May 12, 2014 at 4:40 pm

Hey all,

I’m trying to generate a list of attendees via the JSON API in PHP. Here’s the code I’m using to display the results:

<code>&lt;?php 

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

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, &quot;http://triitforlife.com/espresso-api/v1/*REMOVED*);

// 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[&#039;body&#039;][&#039;session_key&#039;];

$q = &#039;Event.id__LIKE=174&#039;; 

$endpoint = &#039;http://triitforlife.com/espresso-api/v1/registrations/&#039; . $sesskey . &#039;.pretty_json?&#039; . $q;

echo $endpoint . &#039;&lt;br /&gt;&lt;br /&gt;&#039;;
$session = curl_init($endpoint);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($session);
curl_close($session);
$search_results = json_decode($data, true);
if ($search_results === NULL) die(&#039;Error parsing json&#039;);

$event_name = $search_results-&gt;body-&gt;Registrations-&gt;Event-&gt;id;
foreach ($search_results[&#039;body&#039;][&#039;Registrations&#039;] as $attendees)
{
foreach ($attendees as $attendee){
$aname = $attendee[&#039;firstname&#039;].&quot; &quot;.$attendee[&#039;lastname&#039;];
}
echo &quot;&lt;br&gt;&quot;;
}
?&gt;</code>

This “works” technically but the “a21” at the beginning of each line has me stumped. It looks like this:

a21MaryJo
a21Lois
a21Tanja
a21Rogina
a21Lori

How do I get rid of the extra characters?

You can see the result here: http://triitforlife.com/test.php

And the raw JSON here: http://triitforlife.com/espresso-api/v1/registrations/kcmnv6nzwa.pretty_json?Event.id__LIKE=174

Thanks!
Mike

  • This topic was modified 5 years, 4 months ago by  Tony. Reason: Remove login info


Mike Heath

May 12, 2014 at 4:42 pm

Wow that code didn’t format well at all. Let’s try this. <pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”><?php

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

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

// 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'];

$q = 'Event.id__LIKE=174';

$endpoint = 'http://triitforlife.com/espresso-api/v1/registrations/&#039; . $sesskey . '.pretty_json?' . $q;

echo $endpoint . '<br /><br />';
$session = curl_init($endpoint);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($session);
curl_close($session);
$search_results = json_decode($data, true);
if ($search_results === NULL) die('Error parsing json');

$event_name = $search_results->body->Registrations->Event->id;
foreach ($search_results['body']['Registrations'] as $attendees)
{
foreach ($attendees as $attendee){
//$aname = $attendee['firstname']." ".$attendee['lastname'];
print_r ($attendee['firstname']);
}
echo "<br>";
}
?>

  • This reply was modified 5 years, 4 months ago by  Tony. Reason: removed login details


Tony

  • Support Staff

May 13, 2014 at 6:02 am

Hi Mike,

Looking at your code, I looks like there is an extra foreach loop.

foreach ($search_results['body']['Registrations'] as $attendees)
{
foreach ($attendees as $attendee){
//$aname = $attendee['firstname']." ".$attendee['lastname'];
print_r ($attendee['firstname']);
}
echo "<br>";
}

I do not think this is needed:

foreach ($attendees as $attendee){
//$aname = $attendee['firstname']." ".$attendee['lastname'];
print_r ($attendee['firstname']);
}

So something like:

foreach ($search_results['body']['Registrations'] as $registration)
{
print_r ($registration['Attendee']['firstname']);
echo "<br>";
}

Will output the firstname for the attendee within each registration.

The support post ‘Extra Characters in JSON API result set, using PHP.’ 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.

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3

Documentation for Event Espresso 4

Status: closed

Updated by  Tony 5 years, 4 months ago ago

Topic Tags

Tagged: , ,

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!