CJ API Product Catalog Search Service using REST

Discussion in 'Commission Junction' started by lolojono, Jun 30, 2011.

  1. #1
    Hello everyone, I am a newbie when it comes to using the CJ APIs to try to get the information I require, so I managed to find some code that someone else had written and modified it slightly. It seems error-free and should in theory work. Here is the code:

    CJ_API_call_test.php

    <?php
    // Commission Junction Product Catalog Search Service (REST) API
    if(!isset($_GET["test"]))
    {
      $keyword_string = "Mini";
    }
    else
    {
      $keyword_string = $_GET["test"];
    }
    $keyword_string = urlencode($keyword_string); // handles spaces etc.
    $count = "2";
    
    // send request to API
    $cURL = curl_init();
    curl_setopt($cURL, CURLOPT_URL, 'http://xxx/CJ_API_test.php?keyword='.$keyword_string.'&amp;max='.$count);
    curl_setopt($cURL, CURLOPT_HEADER, 0); // we do not want the header unless troubleshooting
    curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
    $strPage = curl_exec($cURL);
    curl_close($cURL);
    echo($strPage);
    ?>
    PHP:
    CJ_API_test.php

    <?php
    $websiteid= "xxx"; // I've used the correct one
    $CJ_DevKey= xxx"; // I've used the correct one here too
    $currency="USD";
    $advs="joined"; // results from (joined), (CIDs), (Empty String), (notjoined)
    // begin building the URI and GETting variables passed
    $targeturl="https://product-search.api.cj.com/v2/product-search?";
    if (isset($_GET["keyword"]))
    {
    $keywords = $_GET["keyword"];
    $keywords = urlencode($keywords);
    $targeturl.="&amp;keywords=$keywords";
    }
     
    if (isset($_GET["max"]))
    {
    $maxresults = $_GET["max"];
    $targeturl.="&amp;records-per-page=".$maxresults;
    }
     
    $targeturl.="&amp;website-id=$websiteid";
    $targeturl.="&amp;advertiser-ids=$advs";
    $targeturl.="&amp;currency=$currency";
    // end building targeturl
     
    $ch = curl_init($targeturl);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_DevKey)); // send development key
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($ch);
    $xml = new SimpleXMLElement($response);
    curl_close($ch);
     
    if ($xml)
    {
    foreach ($xml->products->product as $item) {
    $link  = $item->xpath('buy-url');
    $link  = (string)$link[0];
     
    $title = $item->xpath('name');
    $title = (string)$title[0];
     
    $imgURL = $item->xpath('image-url');
    $imgURL = (string)$imgURL[0];
     
    $price = $item->xpath('price');
    $price = '<br />$'.number_format($price[0],2,'.',',');
     
    $merchantname = $item->xpath('advertiser-name');
    $merchantname = (string)$merchantname[0];
     
    $description = $item->xpath('description');
    $description = (string)$description[0];
     
     
    if($link != "")
    $results .="<div id=\"product\">
    <div id=\"product_img\"><a href=\"$link\" target=\"_blank\"><img src=\"$imgURL\"/></a></div>
    <div id=\"product_link\"><a href=\"$link\" target=\"_blank\">$title</a></div>
    <div id=\"product_desc\">".$description."</div>
    <div id=\"product_price\"><a href=\"$link\" target=\"_blank\">".$price."</a></div>
    </div><br /><br />";
     
    }
    }
     
    if ($results == '') { $results = "<div id=\"product\">There are no available products at this time or no search parameters were specified.  Please try again later.</div>"; }
     
    print $results;
    // print curl_error($ch);
    print "\n\r".$_GET["keyword"]."\n\r".$targeturl."\n\r";
    var_dump(simplexml_load_string($response));
    ?>
    PHP:
    Now when I run the file CJ_API_call_test.php I get this result:

    The problem is that I can't see the use of an invalid key, so what is going on?

    Thanks.
     
    lolojono, Jun 30, 2011 IP
  2. FunkyFresh

    FunkyFresh Peon

    Messages:
    499
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I tried your code and I am getting the same thing thing. My key is 261 characters long, is that the right key?
     
    FunkyFresh, Oct 2, 2011 IP
  3. uleesgold

    uleesgold Member

    Messages:
    288
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #3
    This thread is old now but I'm having a similar problem. Only in my case it returns no results with this code example:
    <?php
    $websiteid= "MySiteID";
    // register for your developer's key here: http://webservices.cj.com/ (input dev key below)
    $CJ_DevKey= "MyDeveloperKey";
    $currency="USD";
    $advs="joined"; // results from (joined), (CIDs), (Empty String), (notjoined)
    // begin building the URL and GETting variables passed
    $targeturl="https://product-search.api.cj.com/v2/product-search?";
    if (isset($_GET["keyword"]))
    {
    $keywords = $_GET["keyword"];
    $keywords = urlencode($keywords);
    $targeturl.="&amp;keywords=$keywords";
    }
    if (isset($_GET["max"]))
    {
    $maxresults = $_GET["max"];
    $targeturl.="&amp;records-per-page=".$maxresults;
    }
    $targeturl.="&amp;website-id=$websiteid";
    $targeturl.="&amp;advertiser-ids=$advs";
    $targeturl.="&amp;currency=$currency";
    // end building targeturl
    $ch = curl_init($targeturl);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_DevKey)); // send development key
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($ch);
    $xml = new SimpleXMLElement($response);
    curl_close($ch);
    if ($xml)
    {
    foreach ($xml-<span>></span>products-<span>></span>product as $item) {
    $link = $item-<span>></span>xpath('buy-url');
    $link = (string)$link[0];
    $title = $item-<span>></span>xpath('name');
    $title = (string)$title[0];
    $imgURL = $item-<span>></span>xpath('image-url');
    $imgURL = (string)$imgURL[0];
    $price = $item-<span>></span>xpath('price');
    $price = '
    $'.number_format($price[0],2,'.',',');
    $merchantname = $item-<span>></span>xpath('advertiser-name');
    $merchantname = (string)$merchantname[0];
    $description = $item-<span>></span>xpath('description');
    $description = (string)$description[0];
    if($link != "")
    $results .="
    $title
    ".$description."
    ".$price."
    ";
    }
    }
    if ($results == '') { $results = "
    There are no available products at this time or no search parameters were specified. Please try again later.
    "; }
    print $results;
    ?>
    PHP:
    If you use the CJ API, can you please post an example that works? I have to admit that I did just copy this example from a website I found when Googling the problem. A couple of others didn't work either (whether it was on my PC or to a remote server that I uploaded it to.) I do have cURL installed. Ideally I'd prefer to use JavaScript for this, but I don't know a whole lot about coding to begin with.
     
    uleesgold, Dec 10, 2014 IP