Webservices PHP CURL Help

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.

What I did is just saved the code into php file and opened it using my browser and the output is all text. Am I doing something wrong ?

When you say “using your browser” do you mean that’s what you saw on your screen or what was shown in view-source? eg.

<tag>text</tag>

will only show

text

on the screen. The tags can be seen in view-source. Or is view-source only text?

AFAIK there are 2 ways you can go about it.

You can change your Apache settings so all XML files will be parsed by the PHP engine. You can then give that PHP file an .xml extension.

Or you can use htaccess to rewrite all requests for whatever.xml to go to whatever.php

I guess it depends on how many XML files you have, whether or not you want to have PHP parse all of them, and how much you want to mess with your htaccess file.

The Apache Configuration forum is the best place to go for getting help with that.

You are right, when I look at the source it was xml but when I look in browser it wasn’t.

You can fix this issue by adding header

header('content-type: text/xml; charset=utf-8'); 

Now my problem is , I have www.example.com/1.php file giving me xml output. I want to rename it as xml instead of php.

I tried to rename it but it didn’t work. Any suggestions ?

There is nothing to fix. If you look at view-source you will see that it is XML

<?xml version="1.0" encoding="UTF-8"?>
<cj-api><links total-matched="206" records-returned="100" page-number="1"><link><advertiser-id>1607965</advertiser-id><advertiser-name>101Phones</advertiser-name><category>consumer electronics</category><click-commission>0.0</click-commission><creative-height>0</creative-height><creative-width>0</creative-width><category>consumer electronics</category><click-commission>0.0</click-commission><creative-height>0</creative-height><creative-width>0</creative-width><language>en</language><lead-commission></lead-commission><link-code-html><a href="http://www.jdoqocy.com/click-4120339-10804841">Shop the Best Cordless Phones Under $50 &amp; Free Shipping at 101Phones.com!</a><img src="http://www.tqlkg.com/image-4120339-10804841" width="1" height="1" border="0"/></link-code-html>

Hi,

Thanks for your reply. I see only text on the screen. It’s not in the usual xml structure format. please visit this link for the sample I created. www.uedeals.com/cj/cj3.php

I checked it on IE and it is structured like xml. However, any other browser other than IE it just shows text. So how can I fix this issue ?

Secondly I want to save the response into a xml file on my server somethinng like cj.xml and to be updated dynamically.