Hi,

I have used the following script to consume REST webservices provided by Commission Junction. I'm able to get the response but the response is not in xml format.

<?php

$targeturl="https://support-services.api.cj.com/v2/countries";

$CJ_DevKey= "xxxxxxxxx";

// return xml feed from CJ

$ch = curl_init($targeturl);
curl_setopt($ch, CURLOPT_POST, FAlSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_DevKey));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);

echo $response; // will print in browser all country codes in an xml format

/*

ADD YOUR OWN CODE HERE TO DO WHAT YOU WANT WITH THE RESPONSE.  MAYBE SAVE RESULTS TO A FILE OR THE PARSE THE RESULTS INTO A DATABASE?

*/

?>

I'm just confused. Isn't that the response when using REST webservices is always in xml format. Please correct me if I'm wrong.

Would someone please have a look at this script and suggest me what I need to do in order to get the response in xml ? I want to save the response in xml file and then process it later.

    Sorry forgot to add you can see my current sample output here which is in plain text.

    www(dot)uedeals(dot)com/cj/cj3.php

      The response actually is XML. However, unless the browser gets an HTTP content-type header specifying text/xml, or possibly the URL ends with .xml, it has no way of knowing that it should present the contents as an XML file, and proceeds to its standard behaviour of showing only tag contents, not the tags themselves.
      Rclick that page -> show source.

      And if the php script is your own, add this before any output is sent

      header('content-type: text/xml; charset=utf-8'); # or whatever charset you use.
      

        Thanks for your reply. yeah I have added the header and it worked.

        But to make it much better I want to end the file name in xml. How can I do that ?

          You could change your script name from .php to .xml but that would also require that you tell the server to parse XML files as PHP files.

          Or, you could serve the data as attachment.
          file_1.html, with link to your php script

          <a href="http://example.com/cj/cj3.php">Get xml</a>
          

          cj3.php

          $string = get_xml_data();
          
          header('content-disposition: attachment; filename=data.xml');
          header('content-type: text/xml');
          echo $string;
          

          And when you click the link in file_1.html, you don't get a new page, but just a download/open with.. dialog (actual handling may differ between browsers).

            I prefer the first method seems easy. I went ahead and renamed the file to xml but gave me an error.

            So how do I tell the server to parse XML files as PHP files.? The reason why I want to have .xml extension is because I want to pass this file to other tools for more processing.

              WWW_9hub_Net wrote:

              The reason why I want to have .xml extension is because I want to pass this file to other tools for more processing.

              Okay... but why does it matter what the file extension is?

              Regardless, a much easier approach would probably be to use mod_rewrite (assuming it's available) to redirect requests for "data.xml" to "data.php", making it appear as if you're accessing a .xml file.

                Write a Reply...