Parsing XML with PHP: CJ REST API

Discussion in 'PHP' started by Friendclk, Sep 4, 2009.

  1. #1
    I am beginning to experiment with the Commission Junction API. I want to use the REST method as the SOAP method is being slowly phased out. After some searching, I have assembled some PHP code that successfully connects to Commission Junctions REST product search api and returns results. I am having a frustrating time saving the response I get to an XML file, or at least an variable or array that I can easily read and manipulate. Can someone help me find the next step here. Thanks in advance! ;)

    <?php
    $CJ_ID = "my developer key goes here";
    $url = "https://product-search.api.cj.com/v2/product-search?website-id=3545042&serviceable-area=US&keywords=hotel";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_ID));
    $curl_results = curl_exec($ch);
    
    $meta_tags = rawurldecode($curl_results);
    
    $myFile = "datafeed.xml";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $meta_tags);
    fclose($fh);
    ?>
    PHP:

     
    Last edited: Sep 5, 2009
    Friendclk, Sep 4, 2009 IP
  2. Friendclk

    Friendclk Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    As it would turn out this code actually works. When I tried to read the XML file from within Firefox, I was getting all kinds of errors, but when I downloaded the XML file I realized Firefox was simply thrown off by the XML. So, hopefully someone else searches for this code and finds it helpful. Thanks everyone!
     
    Friendclk, Sep 5, 2009 IP