SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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 Code:
    <?php

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

    // return xml feed from CJ

    $ch curl_init($targeturl);
    curl_setopt($chCURLOPT_POSTFAlSE);
    curl_setopt($chCURLOPT_HTTPHEADER, array('Authorization: '.$CJ_DevKey));
    curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
    $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 ?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,262
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    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?

  3. #3
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,262
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    There is nothing to fix. If you look at view-source you will see that it is XML
    Code 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>&lt;a href="http://www.jdoqocy.com/click-4120339-10804841"&gt;Shop the Best Cordless Phones Under $50 &amp; Free Shipping at 101Phones.com!&lt;/a&gt;&lt;img src="http://www.tqlkg.com/image-4120339-10804841" width="1" height="1" border="0"/&gt;</link-code-html>

  5. #5
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    PHP Code:
    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 ?

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,262
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •