sending xml response when a url is hit in php

25,721

Solution 1

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);

Solution 2

You just need to specify some header within your script which tells the browser/client to handle this content properly! Other than that you have to concatenate rather than reassign your $response var ;)

In php this should do the job:

header("Content-type: text/xml;charset=utf-8");  
Share:
25,721
Mofizur
Author by

Mofizur

Updated on July 05, 2022

Comments

  • Mofizur
    Mofizur almost 2 years

    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?

  • Mofizur
    Mofizur over 11 years
    can't understand would you please explain in details based on my scenario.
  • Ivo
    Ivo over 11 years
    If you echo your $controller->sendResponse($type,$cause) (btw you have to echo it, otherwise it gets just generated but not printed) it doesnt mean the client knows what you're sending in.. most of the times it expects text or html! so you just have to tell what your content is ;)
  • Mofizur
    Mofizur over 11 years
    Thanks a lot for your prompt reply.