Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cURL with XML and PHP

Posted on 2013-02-01
9
Medium Priority
?
395 Views
Last Modified: 2013-02-12
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
$ch = curl_init("http://#URL#:8080/WhyQ/WhyQAppServlet?method=showUser&userPhoneNo=". $_POST["loginMobile"] );

Open in new window


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>

Open in new window



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
?>

Open in new window

0
Comment
Question by:DanteAdmin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38843358
You may be able to read the XML document from the service with file_get_contents() instead of cURL.  Simpler syntax, but less control of the connection.  You can use SimpleXML_Load_String() to make an object from an XML document.  You may be able to use SimpleXML_Load_File() to read the XML directly from the service.

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

<?php // RAY_temp_danteadmin.php
error_reporting(E_ALL);
echo '<pre>';

// THE XML DOCUMENT WOULD COME FROM SOME EXTERNAL SERVICE
$xml = <<<EOD
<?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>
EOD;

// SHOW THE XML
echo htmlentities($xml);
echo PHP_EOL;

// CREATE AND VISUALIZE AN OBJECT
$obj = SimpleXML_Load_String($xml);
var_dump($obj);
echo PHP_EOL;

// GET NAME AND PHONE
$n = (string)$obj->name;
$p = (string)$obj->phoneNumber;
echo "$n $p";
echo PHP_EOL;

// GET QUEUE POSITIONS
$ques = $obj->Queues;
foreach ($ques->Queue as $q_obj)
{
    $qn = (string)$q_obj->queueName;
    $qp = (string)$q_obj->queuePosition;
    echo "$qn $qp";
    echo PHP_EOL;
}

Open in new window

0
 

Author Comment

by:DanteAdmin
ID: 38843377
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</phoneNumber>
<Queues>
<Queue>
<queueName>The Pig Bar</queueName>
<queuePosition>0</queuePosition>
</Queue>
<Queue>
<queueName>Exotic Restaurant</queueName>
<queuePosition>0</queuePosition>
</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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38843563
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?
0
The Ideal Solution for Multi-Display Applications

Check out ATEN’s VS1912 12-Port DP Video Wall Media Player at InfoComm 2017. Kerri describes how easy it is to design creative video walls in asymmetric layouts and schedule detailed playlists ahead of time with its advanced scheduling feature.

 

Author Comment

by:DanteAdmin
ID: 38843598
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 ?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38843672
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?
0
 

Author Comment

by:DanteAdmin
ID: 38858362
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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38866044
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.
0
 

Accepted Solution

by:
DanteAdmin earned 0 total points
ID: 38867016
0
 

Author Closing Comment

by:DanteAdmin
ID: 38879543
After some research I found another way of setting the variables to connect, see solution link
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question