Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending xml response when a url is hit in php

Tags:

php

xml

response

I would like to send a xml response when a url is hit with parameters like http://gmail.com/cb/send/send.php?user_name=admin1&password=test123&subscriber_no=1830070547&mask=Peter&sms='Test SMS'

When one will hit this link then a response will be given which will like

 public function sendResponse($type,$cause) {
    $response = '<?xml version="1.0" encoding="utf-8"?>';
    $response = $response.'<response><status>'.$type.'</status>';
            $response = $response.'<remarks>'.$cause.'</remarks></response>';
            return $response;
 }

I am calling this method from my controller file and just echo the value. will the hitter will get this response?

<?php
......
......
  echo $sendResponse($type,$cause);
 ?>

Will user will get resonse by this echo?

like image 483
Mofizur Avatar asked Nov 20 '12 10:11

Mofizur


1 Answers

return alone won't sending anything to the client. If you are echoing the result of sendResponse() then yes, the client will receive the XML:

echo sendResponse($type,$cause);

Notice I removed the $ from the sendResponse call - PHP will assume it's a variable if you use the $.

It's recommended to add a header to tell the client that XML is being sent and the encoding, but it's not essential for the transport of the XML:

header("Content-type: text/xml; charset=utf-8");

You can use the concatenation character . after you declared the XML header:

 public function sendResponse($type,$cause) {

    $response = '<?xml version="1.0" encoding="utf-8"?>';
    $response .= '<response><status>'.$type.'</status>';

            $response = $response.'<remarks>'.$cause.'</remarks></response>';
            return $response;
 }

 ....
 ....

 header("Content-type: text/xml; charset=utf-8");
 echo sendResponse($type,$cause);
like image 89
MrCode Avatar answered Oct 27 '22 01:10

MrCode