Link to home
Start Free TrialLog in
Avatar of axessJosh
axessJosh

asked on

example of response using Curl and PHP

We have a new tool for our church and I'm using their API to connect which for a few things has worked fine.  I'm needing some guidance though on just some basic information on what to do next.

They provide info on the API service, and parameters which I have been able to use fine.

My question is what to do with the XML example that is sent as well?  I am connecting and getting data back as an example below:

object(SimpleXMLElement)#1 (2) { ["request"]=> object(SimpleXMLElement)#2 (1) { ["parameters"]=> object(SimpleXMLElement)#4 (1) { ["argument"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(2) { ["value"]=> string(21) "group_profile_from_id" ["name"]=> string(3) "srv" } } [1]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(2) { ["value"]=> string(3) "136" ["name"]=> string(2) "id" } } } } } ["response"]=> object(SimpleXMLElement)#3 (4) { ["service"]=> string(21) "group_profile_from_id" ["service_action"]=> string(7) "execute" ["availability"]=> string(6) "public" ["groups"]=> object(SimpleXMLElement)#4 (2)

Open in new window


I'd like to specify which information I want to display on our page, but cannot figure out how.

Here is what I tried, but it returns blank.

$xml = simplexml_load_string($data);


if(is_array($xml)) {
foreach($xml->item as $value){
    echo trim($value->full_name)."<br/>\n";
}

Open in new window

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

That looks like a debugging dump of the data (like someone did a var_dump on the object). What does your code look like for the operation / curl call?
Avatar of axessJosh
axessJosh

ASKER

I'll have to login to another computer to get that.

That is a var_dump() that i setup.

What would clarify for me is how to break down the child nodes of the XML document.

for instance:

$xml->resource->groups->group as $value

however, there are child nodes of <group> that I'd like to get values for but it doesn't seem I can drill down that far.
You're headed in the right direction if you're using the PHP SimpleXML functions to create an object from the XML document.  If you want to post the XML document in the code snippet or if you want to post a URL that will allow us to retrieve the XML document, I'll be glad to show you how to parse it.  But let's start with the actual XML and not an abstract representation.  The examples will make more sense if you can see the XML, the object, and the code all at the same time.

One thing that is good about parsing from a PHP object... Eventually everyone will figure out that JSON is better than XML and they will want to move to JSON for the data transport language.  When they do that, your PHP object-oriented programming will need little if any change to incorporate the JSON in place of the XML.
Here is the XML doc Ray.

It looks as though I have to "drill down" the heirarchy of the nodes to get returned the elements that I want.

<?xml version="1.0" encoding="UTF-8"?>
<ccb_api>
 <request>
 <parameters>
 <argument value="group_profiles" name="srv"/>
 <argument value="2010-02-01" name="modified_since"/>
 </parameters>
 </request>
 <response>
 <service>group_profiles</service>
 <service_action>execute</service_action>
 <availability>public</availability>
 <groups count="34">
 <group id="1">
 <name>Entire Church Group</name>
 <description>Everyone is a part of the church. This is the group for the entire church.</description>
 <image>https://s3.amazonaws.com/ccbchurch/40622/pics/g_1?AWSAccessKeyId=AKIAJ4CISARDRJPE4ERQ&amp;Expires=1
390930061&amp;Signature=lCcx73c682HA3blh5LbBbtrl7VU%3D</image>
 <campus id="1">Church of Cucumbers</campus>
 <main_leader id="1">
 <first_name>Larry</first_name>
 <last_name>Cucumber</last_name>
 <full_name>Larry Cucumber</full_name>
 <email>test@test.com</email>
 <phones>
 <phone type="contact">(012) 345-6789</phone>
 </phones>
 </main_leader>
 <leaders/>
 <participants/>
 <group_type id="5">Church</group_type>
 <department id="1">hello</department>
 <area id="1">Church</area>
 <calendar_feed>webcal://testing_stable.ccbchurch.com/group_calendar.ics?id=1&amp;tk=939102BA159F102DA85800
19B9B51440</calendar_feed>
 <registration_forms/>
 <current_members>56</current_members>
 <group_capacity>Unlimited</group_capacity>
 <addresses>
 <address type="meeting">
 <name>Meeting Address</name>
 <street_address>Meeting Address
Street</street_address>
 <city>City</city>
 <state></state>
 <zip></zip>
 <longitude></longitude>
 <latitude></latitude>
 <line_1>Meeting Address
Street</line_1>
 <line_2>City</line_2>
 </address>
 </addresses>
 <meeting_day id="0"></meeting_day>
 <meeting_time id="0"></meeting_time>
 <childcare_provided>false</childcare_provided>
 <interaction_type>Annoucement Only</interaction_type>
 <membership_type>Open to All</membership_type>
 <notification>false</notification>
 <user_defined_fields/>
 <listed>true</listed>
 <public_search_listed>false</public_search_listed>
 <inactive>false</inactive>
 <creator id="1">Larry Cucumber</creator>
 <modifier id="1">Larry Cucumber</modifier>
 <created>2003-11-26 13:03:43</created> <modified>2011-12-13 08:38:11</modified>
 </group>
 ...
 <group id="41">
 <name>New group created via API</name>
 <description>None</description>
 <image>https://cdn3.ccbchurch.com/preSTABLE/images/group-default.gif</image>
 <campus id="1">Church of Cucumbers</campus>
 <main_leader id="1">
 <first_name>Larry</first_name>
 <last_name>Cucumber</last_name>
 <full_name>Larry Cucumber</full_name>
 <email>test@test.com</email>
 <phones>
 <phone type="contact">(012) 345-6789</phone>
 </phones>
 </main_leader>
 <leaders/>
 <participants/>
 <group_type id="1">Care / Small Group</group_type>
 <department id="0"></department>
 <area id="0"></area>
 <calendar_feed>webcal://yourchurch.ccbchurch.com/group_calendar.ics?id=41&amp;tk=VVNY5RHV2V5PBJP3AFCRBR54P
A2C7FHM</calendar_feed>
 <registration_forms/>
 <current_members>1</current_members>
 <group_capacity>Unlimited</group_capacity>
 <addresses/>
 <meeting_day id="0"></meeting_day>
 <meeting_time id="0"></meeting_time>
 <childcare_provided>false</childcare_provided>
 <interaction_type>Members Interact</interaction_type>
 <membership_type>Invitation or Request Required</membership_type>
 <notification>false</notification>
 <user_defined_fields/>
 <listed>true</listed>
 <public_search_listed>false</public_search_listed>
 <inactive>false</inactive>
 <creator id="0">System User</creator>
 <modifier id="0">System User</modifier>
 <created>2014-01-28 10:25:57</created>
 <modified>2014-01-28 10:25:57</modified>
 </group>
 </groups>
 </response>
</ccb_api>

Open in new window


I was able to get the first bit of items I was looking for, however, can I have multiple loops parsing to get more data higher up in the XML data?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<?php // demo/temp_axessjosh.php
error_reporting(E_ALL);
echo '<pre>' . PHP_EOL;

/**
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28412382.html#a40018768
 */

// TEST DATA FROM THE POST AT EE
$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<ccb_api>
 <request>
 <parameters>
 <argument value="group_profiles" name="srv"/>
 <argument value="2010-02-01" name="modified_since"/>
 </parameters>
 </request>
 <response>
 <service>group_profiles</service>
 <service_action>execute</service_action>
 <availability>public</availability>
 <groups count="34">
 <group id="1">
 <name>Entire Church Group</name>
 <description>Everyone is a part of the church. This is the group for the entire church.</description>
 <image>https://s3.amazonaws.com/ccbchurch/40622/pics/g_1?AWSAccessKeyId=AKIAJ4CISARDRJPE4ERQ&amp;Expires=1
390930061&amp;Signature=lCcx73c682HA3blh5LbBbtrl7VU%3D</image>
 <campus id="1">Church of Cucumbers</campus>
 <main_leader id="1">
 <first_name>Larry</first_name>
 <last_name>Cucumber</last_name>
 <full_name>Larry Cucumber</full_name>
 <email>test@test.com</email>
 <phones>
 <phone type="contact">(012) 345-6789</phone>
 </phones>
 </main_leader>
 <leaders/>
 <participants/>
 <group_type id="5">Church</group_type>
 <department id="1">hello</department>
 <area id="1">Church</area>
 <calendar_feed>webcal://testing_stable.ccbchurch.com/group_calendar.ics?id=1&amp;tk=939102BA159F102DA85800
19B9B51440</calendar_feed>
 <registration_forms/>
 <current_members>56</current_members>
 <group_capacity>Unlimited</group_capacity>
 <addresses>
 <address type="meeting">
 <name>Meeting Address</name>
 <street_address>Meeting Address
Street</street_address>
 <city>City</city>
 <state></state>
 <zip></zip>
 <longitude></longitude>
 <latitude></latitude>
 <line_1>Meeting Address
Street</line_1>
 <line_2>City</line_2>
 </address>
 </addresses>
 <meeting_day id="0"></meeting_day>
 <meeting_time id="0"></meeting_time>
 <childcare_provided>false</childcare_provided>
 <interaction_type>Annoucement Only</interaction_type>
 <membership_type>Open to All</membership_type>
 <notification>false</notification>
 <user_defined_fields/>
 <listed>true</listed>
 <public_search_listed>false</public_search_listed>
 <inactive>false</inactive>
 <creator id="1">Larry Cucumber</creator>
 <modifier id="1">Larry Cucumber</modifier>
 <created>2003-11-26 13:03:43</created> <modified>2011-12-13 08:38:11</modified>
 </group>
 ...
 <group id="41">
 <name>New group created via API</name>
 <description>None</description>
 <image>https://cdn3.ccbchurch.com/preSTABLE/images/group-default.gif</image>
 <campus id="1">Church of Cucumbers</campus>
 <main_leader id="1">
 <first_name>Larry</first_name>
 <last_name>Cucumber</last_name>
 <full_name>Larry Cucumber</full_name>
 <email>test@test.com</email>
 <phones>
 <phone type="contact">(012) 345-6789</phone>
 </phones>
 </main_leader>
 <leaders/>
 <participants/>
 <group_type id="1">Care / Small Group</group_type>
 <department id="0"></department>
 <area id="0"></area>
 <calendar_feed>webcal://yourchurch.ccbchurch.com/group_calendar.ics?id=41&amp;tk=VVNY5RHV2V5PBJP3AFCRBR54P
A2C7FHM</calendar_feed>
 <registration_forms/>
 <current_members>1</current_members>
 <group_capacity>Unlimited</group_capacity>
 <addresses/>
 <meeting_day id="0"></meeting_day>
 <meeting_time id="0"></meeting_time>
 <childcare_provided>false</childcare_provided>
 <interaction_type>Members Interact</interaction_type>
 <membership_type>Invitation or Request Required</membership_type>
 <notification>false</notification>
 <user_defined_fields/>
 <listed>true</listed>
 <public_search_listed>false</public_search_listed>
 <inactive>false</inactive>
 <creator id="0">System User</creator>
 <modifier id="0">System User</modifier>
 <created>2014-01-28 10:25:57</created>
 <modified>2014-01-28 10:25:57</modified>
 </group>
 </groups>
 </response>
</ccb_api>
EOD;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// ITERATE OVER THE ELEMENTS OF THE OBJECT
foreach ($obj->request->parameters->argument as $arg)
{
//     var_dump($arg);
    $n = $arg->attributes()->name;
    $v = $arg->attributes()->value;
    echo PHP_EOL . "NAME: $n";
    echo PHP_EOL . "VALUE: $v";
}
echo PHP_EOL;

foreach ($obj->response->groups->group as $grp)
{
//    var_dump($grp);
    $n = $grp->name;
    $d = $grp->main_leader->full_name;
    $t = $grp->group_type;
    echo PHP_EOL . "GROUP NAME: $n, LEADER: $d, TYPE: $t";
}

Open in new window

HTH, ~Ray