Posted: 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 ๐ |
|
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: if i paste: what am I doing wrong here?
|
|
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. |
|
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; ?>
|
|
i have code working now to query /registrations , e.g. I have some test registrations in the database. I can count the number of registrations like this: 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 ๐ |
|
ps – obviously i can’t get the hang of the code highlighting here.. although I did try |
|
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){ this prints: a a2 2 5 5 dave davey rather than what i want ie dave davey what are the extra characters? |
|
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 |
|
[code language=”PHP”] |
|
<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”><?php $test=1; |
|
<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”><?php // SET main domain name to use |
|
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 |
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.