Get/Post Requests result in XML - Dealing with that XML?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanjones102
Forum Newbie
Posts: 5
Joined: Sat Aug 21, 2010 12:49 pm

Get/Post Requests result in XML - Dealing with that XML?

Post by ryanjones102 »

Hi All,

I've never used a forum like this before, but hey ho, I've put the books down, and I'm at your mercy...

I've bought this device called an SMS2Printer... anyhow, I'm playing about with it, and essentially I send it a post/get request through a remote API and it has a number of variables... username, password, etc... and "print_message" - the SMS printer remotely will print whatever is in the "print_message". So far so good.

Only problem is, when it prints, the page returned to the user is XML formatted:

<result>
<status>success</status>
</result>

As i've never done anything like this before, I was wondering if anyone could point me in the right direction of how to receive the above and reformat it... i mean, in an ideal world I'd like to run a request over to this printer in the background, and keep the user in my PHP app.

The application is for my brother's takeout restaurant, for online ordering. I'm doing it as a favour for him, but I'm really confused, and I can't let him down!

I tried cURL:

Code: Select all

$url = "https://sms2printer.com/manage/api/";
$msg = "?apiver=1&imei=123456789012345&password=abcdefghi&action=print&print_message=2010-06-21 A Test message sent to the SMS Printer.";
sendcallback($url,$msg);
function sendcallback($scripturl,$message){
$message=urlencode($message);
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, '.$scripturl.$message.');
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
}
But sadly no joy.... anyone any ideas???
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Get/Post Requests result in XML - Dealing with that XML?

Post by josh »

Press expand

Code: Select all

// execute the request & get the response (For POST use Curl)
$xmlString = file_get_contents('http://example.com/XML.xml'); 

 // use the simple xml library to parse the XML for us (http://us.php.net/manual/en/book.simplexml.php)
$structured = new SimpleXMLElement($xmlString );

// you should already know what this does, just outputting the array (technically object)
print_r($structured ); 
Essentially you can convert from an array to an object graph, and back to an XML document with one line of code. When I build up my XML responses I like to "do it by hand" though because I want the formatting to be a precise way. That usually if the end user is supposed to see or edit the XML. If only a machine is going to see the XML, use that simple XML library for both parsing & generating XML.
ryanjones102
Forum Newbie
Posts: 5
Joined: Sat Aug 21, 2010 12:49 pm

Re: Get/Post Requests result in XML - Dealing with that XML?

Post by ryanjones102 »

People like you Josh are too damn clever.

Thank you very much. I can't believe it was that simple.
Post Reply