DanteAdmin
asked on
cURL with XML and PHP
Hi guys, I'm slowly learning cURL and working with XML structure and I need some help. What I'm doing is working to a point but it's not the right way
We have the web service running, and returning XML depending on what requests we send to the system
For example the below call return a Customers position in the queue as an XML output, illustrated below the call
XML which returns:
I'm then doing this to access the data returned, but I do realise I'm saving it to a file, just to then open it with simpleXML ... my question is, is there a better way then I'm doing it where I simple read the XML straight and output it which then gives me more control ... instead of me saving it to a file - I'm sure there is.
And saving it to a file doesn't work either if more then one user is checking it at the same time...
We have the web service running, and returning XML depending on what requests we send to the system
For example the below call return a Customers position in the queue as an XML output, illustrated below the call
$ch = curl_init("http://#URL#:8080/WhyQ/WhyQAppServlet?method=showUser&userPhoneNo=". $_POST["loginMobile"] );
XML which returns:
<?xml version='1.0' encoding='UTF-8'?>
<User>
<id>1</id>
<email>john@doe.com</email>
<name>John Doe</name>
<phoneNumber>0987654321</phoneNumber>
<Queues>
<Queue>
<queueName>The Pig Bar</queueName>
<queuePosition>0</queuePosition>
</Queue>
<Queue>
<queueName>Exotic Restaurant</queueName>
<queuePosition>0</queuePosition>
</Queue>
</Queues>
</User>
I'm then doing this to access the data returned, but I do realise I'm saving it to a file, just to then open it with simpleXML ... my question is, is there a better way then I'm doing it where I simple read the XML straight and output it which then gives me more control ... instead of me saving it to a file - I'm sure there is.
And saving it to a file doesn't work either if more then one user is checking it at the same time...
$ch = curl_init("http://#URL#:8080/WhyQ/WhyQAppServlet?method=showUser&userPhoneNo=". $_POST["loginMobile"] );
$fp = fopen("CustomerQueue.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_exec($ch);
curl_close($ch);
fclose($fp);
// specify url of xml file
$url = "CustomerQueue.xml";
// get xml file contents
$xml = simplexml_load_file($url);
// loop begins
foreach($xml->Queues->Queue as $Customer)
{
echo "<p>";
echo "<tr>";
echo "<td>". $Customer->queueName ."</td>";
//add +1 to position in queue to correct 0 position start
$queuePosition = $Customer->queuePosition=$Customer->queuePosition+1;
echo "<td style='text-align:center;'>". $queuePosition ."</td>";
echo "<td style='text-align:center;'>". $estimatedWait ."</td>";
?>
<?
echo "</tr>";
}
// loop ends
?>
ASKER
Hi Ray
Ok I get all that and how your displaying the data. But my main issue is how I'm currently calling the web services, then saving the respone to an XML file
"CustomerQueue.xml" is created and then put in where you have XML ... I would like to use cURL to get the information as we have that all set up so we can use HTTP_POST against the server
So what I'm really after is how do I get control / being able to access the response I get from the cURL data without saving it to an XML file
There will be multi user issues I'm sure. Let's say we have 300 users wanting to check data at the same time, then that "CustomerQueue.xml" will be created 300 times within a few seconds
Even if that is not a problem I'm sure it's not the best / "correct" way of doing it ?
<code>
$xml = <<<EOD
<?xml version='1.0' encoding='UTF-8'?>
<User>
<id>1</id>
<email>john@doe.com</email >
<name>John Doe</name>
<phoneNumber>0987654321</p honeNumber >
<Queues>
<Queue>
<queueName>The Pig Bar</queueName>
<queuePosition>0</queuePos ition>
</Queue>
<Queue>
<queueName>Exotic Restaurant</queueName>
<queuePosition>0</queuePos ition>
</Queue>
</Queues>
</User>
EOD;
</code>
Is there no way for me to connect to the system in either similar way or different way as I'm doing with cURL, and reading the XML reply ?
Or is this the best way of doing it ? Can I save it to a variable isntead ? Haven't tried it yet but will now.
Ok I get all that and how your displaying the data. But my main issue is how I'm currently calling the web services, then saving the respone to an XML file
"CustomerQueue.xml" is created and then put in where you have XML ... I would like to use cURL to get the information as we have that all set up so we can use HTTP_POST against the server
So what I'm really after is how do I get control / being able to access the response I get from the cURL data without saving it to an XML file
There will be multi user issues I'm sure. Let's say we have 300 users wanting to check data at the same time, then that "CustomerQueue.xml" will be created 300 times within a few seconds
Even if that is not a problem I'm sure it's not the best / "correct" way of doing it ?
<code>
$xml = <<<EOD
<?xml version='1.0' encoding='UTF-8'?>
<User>
<id>1</id>
<email>john@doe.com</email
<name>John Doe</name>
<phoneNumber>0987654321</p
<Queues>
<Queue>
<queueName>The Pig Bar</queueName>
<queuePosition>0</queuePos
</Queue>
<Queue>
<queueName>Exotic Restaurant</queueName>
<queuePosition>0</queuePos
</Queue>
</Queues>
</User>
EOD;
</code>
Is there no way for me to connect to the system in either similar way or different way as I'm doing with cURL, and reading the XML reply ?
Or is this the best way of doing it ? Can I save it to a variable isntead ? Haven't tried it yet but will now.
What part of this do you control on your servers? Do you create the XML documents? Or are you getting those from a web service?
ASKER
Hi Ray
This bit is what calls our web service:
<code>$ch = curl_init("http://#URL#:8080/WhyQ/WhyQAppServlet?method=showUser&userPhoneNo=0987654321");</code>
as you can see this is PHP, using curl, this goes of the a remote web service, which we control, but we don't need any changes on that side, that all works fine and returns what we want.
This returns the XML as per message above if the telephone number is in the system, otherwise it returns a blank XML structure. We have several other commands we can send to the web service and it returns the XML accordingly.
What I'm hoping to achieve is a way to NOT save this to an *.xml file on the web server, but to instantly handle the XML and present it to the user using PHP or anyting else that can work on a PHP page
To answer your last question if we create the XML documents it's yes, we control the web service that returns the XML output from the database. But there's no issues there, the XML returns as it should as far as I can tell ?
This bit is what calls our web service:
<code>$ch = curl_init("http://#URL#:8080/WhyQ/WhyQAppServlet?method=showUser&userPhoneNo=0987654321");</code>
as you can see this is PHP, using curl, this goes of the a remote web service, which we control, but we don't need any changes on that side, that all works fine and returns what we want.
This returns the XML as per message above if the telephone number is in the system, otherwise it returns a blank XML structure. We have several other commands we can send to the web service and it returns the XML accordingly.
What I'm hoping to achieve is a way to NOT save this to an *.xml file on the web server, but to instantly handle the XML and present it to the user using PHP or anyting else that can work on a PHP page
To answer your last question if we create the XML documents it's yes, we control the web service that returns the XML output from the database. But there's no issues there, the XML returns as it should as far as I can tell ?
You may not need cURL at all. Have you tried reading the XML document directly from the web service with SimpleXML_Load_File()?
Is the web service on the same server that you're testing from now? Will that be true in the future when the application is deployed?
Is the web service on the same server that you're testing from now? Will that be true in the future when the application is deployed?
ASKER
Hi Ray
I will try SimpleXML_Load_File() and to read the stream directly - or do I save it in a variable first and then read that ?
Web Service is remote and will be later on as well
Thanks.
I will try SimpleXML_Load_File() and to read the stream directly - or do I save it in a variable first and then read that ?
Web Service is remote and will be later on as well
Thanks.
Usually SimpleXML_Load_File() will work with a fully qualified URL. The advantage of saving the XML string in a variable (See SimpleXML_Load_String() on php.net) is that you can print it out, store your own copy of the test data, etc.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
After some research I found another way of setting the variables to connect, see solution link
This is how I would process the XML document. I doubt if the "more than one user" issue is much of a problem in a real-world environment, unless something is getting confused on the web service. Those multi-user issues confound developers, but rarely cause problems in real life.
See http://www.laprbass.com/RAY_temp_danteadmin.php
Open in new window