Page 1 of 1

PHP5 / XML API help!

Posted: Mon Oct 15, 2007 10:31 am
by oskare100
Hello,
I'm trying to prase and use information I'm getting from an XML API in my system. The user is supposed to be able to choose between the returned agents and then select one of them. When they click "continue" or similar the rows of the agent that the user selected should be saved/assaigned to different PHP variables.

Here is the structure of the XML return:

Code: Select all

<xml>
<system>
<error>
<id>0</id>
<message>Your request was successfully processed.</message>
7</error>
</system>
<response>
<service>
<name>Agents</name>
<version>10</version>
</service>
<data_list_count>2</data_list_count>
</response>
<data_list>
<data index="0">
<agent_list>
<agent index="0">
<name>AGENT & CO IT</name>
<address>STREET ADDRESS 2</address>
<address2/>
<zipcode>500000</zipcode>
<city>TOWN</city>
<agent_number>0661</agent_number>
</agent>
<agent index="1">
<name>ANOTHER AGENT</name>
<address>STREET ADDRESS 5</address>
<address2/>
<zipcode>600000</zipcode>
<city>CITY</city>
<agent_number>0662</agent_number>
</agent>
</agent_list>
</data>
</data_list>
</xml>
Now I need to list the agents so that the user can select one of them.. I've understood that I should use SimpleXML for this since it is the easiest way.

Code: Select all

$xml = simplexml_load_file($request_url) or die("feed not loading");


That about what I've managed to do, I understand that in some way I need to list the different agents.

Code: Select all

foreach ($xml-> ...? ) (
print ?
)


Then I would need to add so that the user can select one of the returned agents and click continue and that my script then can get the rows/information about the selected agent. Maybe HTML POST/GET but I don't know how to make it do that...

I appreciate any help, links, code or whatever I can use to solve this problem.

Thanks
/Oskar

Posted: Mon Oct 15, 2007 12:47 pm
by volka

Code: Select all

<?php
$request_url = 'test.xml';
$xml = simplexml_load_file($request_url) or die("feed not loading");
foreach( $xml->xpath('//agent') as $agent ) {
	echo $agent['index'], ' ' , $agent->agent_number, "<br />\n";
}

echo "agent[index=1]<br />\n";
$index = 1;
foreach( $xml->xpath("//agent[@index='$index']") as $agent ) {
	echo $agent['index'], ' ' , $agent->agent_number, "<br />\n";
}

Posted: Mon Oct 15, 2007 12:58 pm
by shannah
One thing that I found tricky was the the xpath() method is only available in PHP 5.2 and higher. I was trying it in PHP 5.1 - wasn't getting errors, but wasn't getting any results either.

Double check your version of PHP if you aren't getting results on xpath()