Hello all, I am trying to create a shopping comparison site where the user searches for say, an ISBN and a list of prices for that book is displayed. I have figured out how to query Amazon's API through PHP so that a user can search by Title, ISBN, etc and a result is returned to my site. BUT, I cannot figure out how to do the same Commission Junction's API? has anybody had success using commission junction's API? Thanks
Sorry Simon...a bit off the track...I like your idea...so you'll be an affiliate to various affiliate networks, right?...is that the ONLY way you'll be monetizing your site?
Yep - here's a bit of code that will hopefully get you started: function getCJ($params) { $devKey = 'YOUR_DEV_KEY'; $siteKey = '123456'; // the siteID for the site you are running the script on $advertiserIds = '1566996,2007750'; // e.g., I think these are Buy.com USA and CA and SHOP.com - the script will only search on these advertisers, since you don't want to market products from advertisers you don't have a relationship with, right? $currency = 'USD'; $ParamArray = array('developerKey' => $devKey, 'websiteId' => $siteKey, 'advertiserIds' => $advertiserIds, 'currency' => $currency); // parameters I will include in all my calls // some optional parameters if ($params ['keywords']) { $ParamArray ['keywords'] = $params ['keywords']; } if ($params ['upc']) { $ParamArray['upc'] = "00".$params ['upc']; } // end of optional parameters - you might not want to specify UPC or keywords $client = new SoapClient ("https://product.api.cj.com/wsdl/version2/productSearchServiceV2.wsdl"); // oh, you need to be running PHP5 to use these SOAP functions $results = $client->search($ParamArray); // creates an object from the API call, hope you are OK with objects, I'm a bit hazy :) return $results; } Code (markup): Now you will call this function like this: echo getCJ(array('keywords' => 'ipod'); // call the getCJ function with parameters as an array - possible parameters are 'upc' => 'UPC CODE OF PRODUCT' or 'keywords' => 'SEARCH TERMS' Code (markup): Then you would need to parse the object resulting from $results, since $results will just be a simpleXML object. You parse out the items with a foreach, like: foreach ($data->out->products->Product as $Product) { $advertiserName = (string)$Product->advertiserName; $buyUrl = (string)$Product->buyUrl; } // NOT complete code, just an example! Code (markup): Hope that works, I sort of lifted it from a project of mine - it's very cheesy, just the basics but it should get you pointed in the right direction.
Markowe, Thanks for trying to help me out. To run your code, I put it all in one file. When I do this, I get a parse error on line 45. Here is the code I am trying to run: <?php require_once('file:///C:/wamp/www/lib/nusoap.php'); function getCJ($params) { $devKey = '****'; $siteKey = '3584291'; // the siteID for the site you are running the script on $advertiserIds = '10522161'; // e.g., I think these are Buy.com USA and CA and SHOP.com - the script will only search on these advertisers, since you don't want to market products from advertisers you don't have a relationship with, right? $currency = 'USD'; $ParamArray = array('developerKey' => $devKey, 'websiteId' => $siteKey, 'advertiserIds' => $advertiserIds, 'currency' => $currency); // parameters I will include in all my calls // some optional parameters if ($params ['keywords']) { $ParamArray ['keywords'] = $params ['keywords']; } if ($params ['upc']) { $ParamArray['upc'] = "00".$params ['upc']; } // end of optional parameters - you might not want to specify UPC or keywords $client = new SoapClient ("https://product.api.cj.com/wsdl/version2/productSearchServiceV2.wsdl"); // oh, you need to be running PHP5 to use these SOAP functions $results = $client->search($ParamArray); // creates an object from the API call, hope you are OK with objects, I'm a bit hazy :) return $results; ?> <?php foreach ($data->out->products->Product as $Product) { $advertiserName = (string)$Product->advertiserName; $buyUrl = (string)$Product->buyUrl; } // NOT complete code, just an example! ?> <?php echo getCJ(array('keywords' => 'ipod'); // call the getCJ function with parameters as an array - possible parameters are 'upc' => 'UPC CODE OF PRODUCT' or 'keywords' => 'SEARCH TERMS' ?> PHP: I am not too familar with PHP, so any help you can provide will be appreciated. Kre8or, Thank you for the compliment. Yes, that is the only way I foresee monetizing my site.
Sorry, I wrote these pointers under the assumption that they would be used by someone who is OK with PHP and can use the principles to write their own script - it wasn't intended to be complete, functioning code, just to get you on the right track. There are various obvious things missing from this code. If you want full code, I would suggest finding a ready-made plugin or script - I don't have fully-functioning code that I can post here...
I think it is better if I go with the REST request for getting the XML rather than the SOAP request, it seems programmatically less complicated. Now I am running into problems with the REST request. I am trying to get the sample code CJ publishes on their website to run. Here is the sample code: https://product-search.api.cj.com/v2/product-search?website-id=12345678keywords=GPS&serviceable-area=US Code (markup): In addition they mention, " Commission Junction's REST Web Services use the standard HTTP Authorization header to pass authentication information. You must provide your developer key to pass authentication. The request URI for the Publisher Lookup Service API must be properly encoded based on HTML 4 specification form content encoding rules. Please refer to http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4 for the standard specification. " How do I sender a header authorization with PHP? What all needs to be in it? For more info, here is Commission Junction's webpage regarding APIs: http://help.cj.com/en/web_services/web_services.htm#api_hosts.htm Thanks for your help
hello, i'm working with CJ REST api and I can get response from some of merchant and I check why? I try to check with many case of issue and I think I found some thing different. I can't get any response from REST in case of advertiser-sku contain alphanumeric (A-Z) for example N82E16882103419 (newegg.com) I don't know i missing something but when i try to get from buy.com (advertiser-sku is numeric) IT WORK. I need help my site are not update price from CJ except buy.com Please help...
MasterG, How did you send your DeveloperKey to Commission Junction? that is the part I am stuck on. If I could do that then I could try and help you figure out your particular matter. Can you send me the rest URI that you could get to work? Once I have a working version, perhaps I can figure out why the REST request when you try the advertiser SKU doesn't work. Thanks, Simon
Hello Simon, This is my working code to connect with CJ API. $url = CJ_PRODUCT_SEARCH_URL. "website-id=".CJ_WEBID. "&advertiser-ids=".$advertiserIds. "&serviceable-area=US". "&advertiser-sku=".$advertiserSku. "¤cy=USD"; $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); Code (markup): CJ_KEY is your developer key I hope you can work and help me to solve my issue as well. MasterG
MasterG, I couldn't get your code to work. For the URI, I simply used the URI that is located in the Commission Junction Web Services Help page, http://help.cj.com/en/web_services/web_services.htm HTML: Here is how I tried to get your code to run and failed: <?php $url = 'https://product-search.api.cj.com/v2/product-search?website-id=12345678keywords=GPS&serviceable-area=US'; $CJ_KEY = 'your_developer_key_here'; $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); $returnXML = simplexml_load_string($curl_results); var_dump($returnXML); ?> PHP: When I run this code, I get this as the output: bool(false) Can you get the XML to print out? if so, how are you doing it, and can you give me an example of a URI that you know works? For example, the URI that you use when querying buy.com?
Hello Simon, Your CJ API url is correct and I also try again and it still work. My worked code query canon sd1200 from buy.com below and also response xml PHP CODE: $url = "https://product-search.api.cj.com/v2/product-search?website-id=XXXXXXX&advertiser-ids=1566996&serviceable-area=US&advertiser-sku=210790596&keywords=¤cy=USD"; $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); PHP: XML RESPONSE: <?xml version="1.0" encoding="UTF-8"?> <cj-api><products total-matched="1" records-returned="1" page-number="1"><product><ad-id>10387771</ad-id><advertiser-id>1566996</advertiser-id><advertiser-name>Buy.com USA and Buy.com CA</advertiser-name><buy-url>http://www.dpbolvw.net/click-2828945-10387771?url=http%3A%2F%2Fwww.buy.com%2Fprod%2Fcanon-powershot-sd1200-is-10-megapixel-digital-camera-with-3x-optical%2Fq%2Floc%2F33409%2F210790596.html&cjsku=210790596</buy-url><catalog-id>cjo:984</catalog-id><currency>USD</currency><description>Go Ahead, Have All the Fun You Want. When a camera puts a smile on your face the moment you hold it, imagine how great you'll feel when you see your first pictures! The PowerShot SD1200 IS Digital ELPH has everything going for it - exuberant color, the sculptured style of Canon's famed ELPH series - and the innovative know-how that takes you to a whole new level of picture-taking accomplishment. Fun, smart and a style that follows you everywhere.</description><image-url>http://ak.buy.com/db_assets/prod_images/596/210790596.jpg</image-url><in-stock></in-stock><isbn></isbn><manufacturer-name>CANON USA - DIGITAL CAMERAS</manufacturer-name><manufacturer-sku>3453B001</manufacturer-sku><name>Canon PowerShot SD1200 IS 10 Megapixel Digital Camera with 3x Optical Zoom, 2.5 LCD, UA Lens, Face Detection & Optical Image Stabilizer - Dark Gray</name><price>229.99</price><retail-price>229.99</retail-price><sale-price>0.0</sale-price><sku>210790596</sku><upc>00013803106541</upc></product></products></cj-api> Code (markup): In case of return bool(false) i think because of your authorization has some problem. You can try to see error of CURL by echo "Curl error: " . curl_error($ch) PHP:
I seem to remember that when I wrote my code CJ did not offer REST, only SOAP, even though I would also have found REST easier to program for, like Amazon. So maybe it is new and might not yet be working fully?
I'm not sure about this issue. Can you testing on it to help us? I need to fix this issue as soon as possible. I also posted in forum of developer but NO answer. Please help.