Does anyone have any scripts to share that retrieve products using Commission Junction's API that actually work?

1.9k views Asked by At

This example only displayed a blank page for me. This one did as well. I've got the latest version of PHP and cURL set up properly, as far as I know so there shouldn't be any problem at that end. I'd prefer JavaScript to retrieve products but I'm open minded. I happen to not be highly skilled, but I'd like to get my foot in the door.

edit: I will show you the code that doesn't work, and the error it is giving me.

<?php

// Your developer key
$cj_id = "My ID - omitted for privacy.";


// Your website ID
$website_id = "Also removed for privacy.";

// Keywords to search for
$keywords = "credit+card";

// URL to query with cURL
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&amp;keywords=$keywords";

// Initiate the cURL fetch
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Send authorization header with the CJ ID. Without this, the query won't work
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$cj_id));
$result = curl_exec($ch);

// Put the results to an object
$resultXML = simplexml_load_string($result);

// Print the results
print "<pre>";
print_r($resultXML);
print "</pre>";

?>

Now, this is the error that it's giving me.

SimpleXMLElement Object
(
    [error-message] => Invalid Key provided. Valid keys are: advertiser-ids, advertiser-sku, currency, high-price, high-sale-price, isbn, keywords, low-price, low-sale-price, manufacturer-name, manufacturer-sku, page-number, records-per-page, serviceable-area, sort-by, sort-order, upc, website-id
)
2

There are 2 answers

0
Bhavin Toliya On

You have a error in your URL, try this:

$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";

instead of :

$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&amp;keywords=$keywords";
0
NhaHuynh On
<?php
                echo '<pre>';


                $url='https://product-search.api.cj.com/v2/product-search?website-id=your-id-key-here&advertiser-ids=4415206&records-per-page=999&serviceable-area=US';
                $CJ_KEY='0085eb59c8928f028ba5b27bccfe17cdd20cf4e9079b977b2cc6df72752abab9205676a2f7ee67befe9dccab85f656ef46aba49e500faccbf75dfc6e03f655334d/00848a3f9bf0e13525bce27f008d6245c3e42ae80f2d80a8d9d2220807ca386f4b10146cbbcfff06aafb5e49c03a3318213389dee7861abb2dd7229470390a89c9';

                $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_KEY));
                $curl_results = curl_exec($ch);
                $xml = simplexml_load_string($curl_results);

                var_dump($xml);


                // Loop Insert Product to database


                echo '<pre>';

// if you no set: records-per-page=999, default get 50 products latest // advertiser-ids=4415206 is Id of Advertiser in CJ, you can replace other id , Hope helpful for you , good luck !

?>