Skip to content


CJ API/WebService version 2

There was a comment a couple of days ago about CJ WebService Version 2. I did get this to work. I found some good code another programmer put together that access the v2 WSDL. Basically, this is my API test script, so you need to manually add your keywords and advertiser ID in the code, but it’s a proof of concept. I think I’ve abandoned the CJ API. I personally think it’s not that consistent. I often get ‘internal server error’ in the response XML. I think it might be a riskier move to go version 2 for real time calls.. just my two cents.

This version needs PHP5 with SOAP services built into PHP (–enable-soap). Also it’s using try/catch for handling SOAP exception and it’s only available in PHP5.

As I mentioned above, I’m pretty much ditching CJ web services all together. I still use eBay’s API, but CJ’s seems a little off for me. I’m doing initial tests with CJ Data Feeds now, and this really appears to be the direction I need to go.

<?php
try {
        $ini            = ini_set("soap.wsdl_cache_enabled","0");
        $client         = new SoapClient(‘productSearchServiceV2.wsdl’, array(‘trace’=> true));
        $results        = $client->search(
                                        $developerKey   = ‘yourlongdevkey’,
                                        $websiteId              = ’0000000′,
                                        $advertiserIds  = ’0000001′,
                                        $keywords               = ‘your keywords’,
                                        $serviceableArea = ,
                                        $isbn                   = ,
                                        $upc                    = ,
                                        $manufacturerName = ,
                                        $manufacturerSku = ,
                                        $advertiserSku = ,
                                        $lowPrice               = ,
                                        $highPrice              = ’50′,
                                        $lowSalePrice   = ,
                                        $highSalePrice  = ,
                                        $currency               = ,
                                        $sortBy                 = ,
                                        $SortOrder              = ,
                                        $startAt                = 0,
                                        $maxResults     = 100
        );
        // DEBUG
       
       
        echo "<h3>Your search for ‘$keywords’ generated $results->count results</h3>";
       
        foreach ($results->products as $product) {
                if(strlen(trim($product->imageUrl)) > 0){
                        $price = sprintf(‘%01.2f’, $product->price);
                        echo "<br>".$product->advertiserId."<br>";
                        echo "<br>SKU:".$product->sku."<br>";
                        echo "<a href=’$product->buyUrl’><img src =’$product->imageUrl’ height=’70′ ><p>$product->name <br/> Offered by $product->advertiserName($product->advertiserId) at <b>$price ($product->currency)</b></p></a>";
                }
        }
       
} catch (Exception $e){
        echo "<div id=’error’><p>There was an error with your request or the service is unavailable.</p></div>";
        print_r ($e);
}
?>
 

You can download productSearchServiceV2.wsdl here.

s

Incoming search terms:

  • CJ API example
  • cj php api
  • commission junction api script php
  • php cj api
  • commission junction php api integration code
  • cj php example
  • commission junction api php example
  • invalid argument supplied for foreach() in cj api
  • java lang classcastexception cj web services advertiser
  • php code cj web service


Related posts:

  1. CJ web service issues – unable to access WSDL
  2. Simple script for connecting to Commission Junction’s Product Web Service and populating a local database
  3. CJ web service issues – unable to access WSDL (UPDATE)
  4. Amazon Product API: SignatureDoesNotMatch error response
  5. eBay API Change

Posted in Affiliate Marketing, Programming.

Tagged with , , , , .


18 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. admin says

    Hello

    This is untested.. but should work. You can take some snippets of code from this tutorial:
    http://www.money-code.com/node/23

    It uses the nusoap class. You can google for more information on that class. You would need to remove the try/catch handling and use nusoap instead of using PHP5′s built in SOAP object. You might have to tweak with nusoap a bit, but overall it should just ‘work’.

    Hope this helps.
    hanji

  2. jeremy says

    Does this list products based on keywords? What exactly does it do?

  3. admin says

    Yes, this will return products from CJ based off of keywords AND advertisers.

  4. hans says

    How to build it in php4?
    Can you tell me?

  5. hans says

    Right now my hosting use php5,
    I have already used your code above but get error :

    Fatal error: Class ‘SoapClient’ not found in /home/airbase/public_html/trycj.php on line 4

    What wrong with that?

    Thanks for your help.

    Hans

  6. admin says

    That’s a bummer. It looks like your hosting provider does not have PHP5 built with SOAP. You can verify this by creating a test script to get phpinfo()…


    < ?php
    phpinfo();
    ?>

    You need to have –enable-soap listed in the Configure Command block at the top. You might be able to request this, but it would involve recompiling PHP on the server. Here is a link about PHP/SOAP:

    http://us2.php.net/manual/en/ref.soap.php

  7. Chris says

    Warning: Invalid argument supplied for foreach() on line 15. I used your code exactly (of course with my account details). And I used the Porduct search wsdl from CJ.

  8. admin says

    Looks like the return value for the foreach was NOT an array. I would var_dump() the return from the web service. My example doesn’t have error to catch that. It might be a string returning a error code or statement back from CJ.

    hanji

  9. yayak says

    Hi, im using the code just like u mentioned. But instead, i got this error:

    There was an error with your request or the service is unavailable.
    SoapFault Object ( [message:protected] => Fault: java.lang.ClassCastException
    ……..
    ……..
    …….
    [faultstring] => Fault: java.lang.ClassCastException [faultcode] => soap:Server )

    any idea what’s going on? is it error in server side?

  10. admin says

    Hello

    That error is definitely server side. That’s the response from the web server. The error is possibly instantiated from your end. I think CJ doesn’t do a good job with catching and explaining some of those errors.

    My guess it’s a value that you’re passing ‘cast’ is when you have one data type (ie: string) and try to change it into a different data type (ie: number). That could possible be the error.. not sure.

    You may want to post this error on the CJ Developer forum.. good people there. They should be able to get specific about this particular error. I’m not a big fan of the CJ API, seems to have glitch exceptions.. I go with the datafeed now.. works 100% of the time.

    hanji

  11. Stephen says

    I’m looking for a developer that has experience working with these APIs. We’re looking to have a custom application built that merges into our system. Please let me know if you know of any developer interested.

    Kindest regards,

    Stephen

  12. admin says

    Weird. The buyURL should be there. I would see if you can do a var_dump on the return object and see what’s up. It could be related to how you’re reaching down to it, or some other issue. Are you seeing any PHP NOTICE warnings in your logs when you execute.

    Live I’ve mentioned in the past, CJ’s API seems a bit flaky to me. I’ve definitely run into weird things, and don’t really mess with it any more. I much more prefer the datafeeds or using PopShops.

    On your decode question. I would say if it’s encoded when you echo it, I would decode it.

    Thanks!
    hanji

  13. Anonymous says

    Hello,
    Can you please explain what this Advertiser ID is, and also mention where I can find this ID for each Advertiser.
    Thanks in advance.

  14. Scott says

    It definitely works. Thanks a million…I have been beating my head against the wall for a week trying to get it to work and then I found this by a simple google search. Thanks again.

  15. Srikanth says

    Hi there,
    I ‘m just wondering whether anyone know how to use CJ web services in classic ASP.
    Please mail me the code, thank you

  16. admin says

    This should not be a problem. You’ll just need to translate code from PHP to ASP, etc to get this to work. The challenging areas will be finding the objects to connect to the service. Using the results should be easy with FileSystem objects. I think you’d need the http connector.. it’s been a million years since I worked in ASP.

  17. kiran says

    Hi,

    First of all, thanks for this code snippet – saved me a lot of time to build a script that does searches products for 100 keywords and downloads the product data. 2 issues (the rest of the response fields can be output on the browser or written to file successfully) –

    1) The buyURL is returning as blank – is this a known CJ issue, or is something wrong in my script? I cannot sell these products without this parameter, so how do i get it if CJ is not populating it?
    2) Do i need to do a utf8_decode on URLs (which may have special characters such as /, :, &, ?, .) before i can write it to a file or display it on the browser?

    thanks
    kiran

  18. piwshop says

    SOAP-ERROR: Parsing WSDL
    and if I go directly to CJ SOAP, it said
    Warning: Invalid argument supplied for foreach()



Some HTML is OK

or, reply to this post via trackback.

CommentLuv badge