2

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
Jack
  • 21
  • 2

3 Answers3

2

From what I can gather curl doesn't do anything with the data it receives, that's up to you. You need to parse it some how.

A similar question is here: PHP cURL, extract an XML response

Also, the response from a webservice does not have to be xml. It's just common, there are other formats like json, yaml, and html too :)

Community
  • 1
  • 1
0

I get an XML response just fine when accessing https://support-services.api.cj.com/v2/countries. Are you just looking at the raw response in the browser? Of course the browser will interpret XML, so you won't see the XML itself, only the contained data. Try View Source of your browser or htmlentities on the result before outputting it.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

I've tried your code and it's generating xml from what I can see. You need to parse the xml so in the part that says ADD YOUR OWN CODE HERE replace it with:

$parsed_xml = simplexml_load_string($response);

As someone previously mentioned you can view the xml's structure by viewing the source of your code's output in the browser. With that info you can start looking at how to handle it with simplexml.

jcroll
  • 6,875
  • 9
  • 52
  • 66