Fetching CJ Products by storeID with PHP

Discussion in 'Commission Junction' started by kodkod, Aug 28, 2009.

  1. kodkod

    kodkod Registered Member

    Joined:
    Aug 15, 2009
    Messages:
    58
    Likes Received:
    11
    Occupation:
    CTO
    Location:
    Israel
    I have this script laying around for the last couple of weeks and i thought i might share it here also, plus maybe someone will be kind enough to give me some ideas on how to make money out of my scripts :) or for new scripts or course ;)
    So again you will need some PHP knowledge inorder to use this.
    This PHP code let you search CJ for products for a specific store with a php script, it will return an easy object to work on so you can push all your products into a database very easly.
    I've left my database insert query maybe it will help some people.
    This script also save a copy of the product image locally on the server so that CJ will not know how many image impressions are coming from your site.
    Requirements:
    PHP_SOAP extention,
    If you want the image fetching to work you will need to find an include "SimpleImage.php" its an image resizing PHP Class
    PHP:
    <?php
    set_time_limit
    (0);
    include(
    'SimpleImage.php');

    $devkey 'YOUDEVKEYID';
    $mywebsite 'YOURWEBSITEID';
    $advertiserID 'ADVERTISERID';
    $keywords '+Jewlery +Ring -4k -Silver';

    $developerKey $devkey;
    $websiteId $mywebsite;

    $ini ini_set("soap.wsdl_cache_enabled","0");

    try {

        
    $client = new SoapClient("https://product.api.cj.com/wsdl/version2/productSearchServiceV2.wsdl", array('trace'=> true));

        
    //Enter the request parameters for productSearch below.
        //For detailed usage of the parameter values, please refer to CJ Web Services online documentation

        
    $results $client->search(array("developerKey" => $developerKey,
                                                
    "websiteId" => $websiteId,
                                                
    "advertiserIds" => $advertiserID,
                                                
    "keywords" => $keywords,
                                                
    "serviceableArea" => 'US',
                                                
    "upc" => '',
                                                
    "linkType" => '',
                                                
    "manufacturerName" => '',
                                                
    "manufacturerSku" => '',
                                                
    "advertiserSku" => '',
                                                
    "lowPrice" => '',
                                                
    "highPrice" => '',
                                                
    "lowSalePrice" => '',
                                                
    "highSalePrice" => '',
                                                   
    "currency" => 'USD',
                                                 
    "isbn" => '',
                                                   
    "sortBy" => 'price',
                                                 
    "sortOrder" => 'asc',
                                                
    "startAt" => 0,
                                                
    "maxResults" => 1000));

        
    //The entire response structure will be printed in the next line
        
    echo '<pre>';
            
    //print_r($results);
        
    echo '</pre>';
        foreach (
    $results->out->products->Product as $product) {
            
    var_dump($product);
                
                
            
    $filename basename($product->imageUrl);
            
    $realData file_get_contents(urldecode($product->imageUrl));
            if (
    $realData) {
                
    $fp fopen('tempfile.jpg','w+');
                
    fwrite($fp,$realData);
                
    fclose($fp);

                
    $target_path "product_images/{$filename}";

                
    $image = new SimpleImage();
                
    $image->load('tempfile.jpg');
                
    $image->resizeToWidth(230);
                
    $image->save($target_path);
                unset(
    $image);

                
    $image = new SimpleImage();
                
    $image->load('tempfile.jpg');
                
    $imageHeight $image->getHeight();
                
    $imageWidth $image->getWidth();
                if (
    $imageWidth $imageHeight) {
                    
    $image->rotate(90);
                }
                
    $image->resizeToWidth(85);
                
    $image->save(substr($target_path,0,strlen($target_path)-4).'-s.jpg');
                unset(
    $image);


                
    $target_path '/'.$target_path;

                
    $target_path1 substr($target_path,0,strlen($target_path)-4).'-s.jpg';
                                    
                
    $randAdd rand(1,25);
                    
                if (
    trim($product->retailPrice) == '') {
                    
    $product->retailPrice intval($product->price) + $randAdd;
                }            

                
    $query 'INSERT INTO products(adId, advertiserId, advertiserName, buyUrl, catalogId, currency, description, image, inStock, manufacturerName, name, price, retailPrice, sku, catid, image1, AddCartLink, salePrice)
                values (\''
    .mysql_real_escape_string($product->adId).'\',
                \''
    .mysql_real_escape_string($product->advertiserId).'\',
                \''
    .mysql_real_escape_string($product->advertiserName).'\',
                \''
    .mysql_real_escape_string($product->buyUrl).'\',
                \''
    .mysql_real_escape_string($product->catalogId).'\',
                \''
    .mysql_real_escape_string($product->currency).'\',
                \''
    .mysql_real_escape_string($product->description).'\',
                \''
    .$target_path.'\',
                \''
    .mysql_real_escape_string($product->inStock).'\',
                \''
    .mysql_real_escape_string($product->manufacturerName).'\',
                \''
    .mysql_real_escape_string($product->name).'\',
                \''
    .mysql_real_escape_string($product->price).'\',
                \''
    .mysql_real_escape_string($product->retailPrice).'\',
                \''
    .mysql_real_escape_string($product->sku).'\',
                \''
    .$catid.'\',
                \''
    .$target_path1.'\',
                \''
    .$buyCartLink.'\',
                \''
    .mysql_real_escape_string($product->salePrice).'\')';            
                echo 
    $query;
                
    mysql_query($query);
                echo 
    '<br />';
                
    ob_flush();
                
    flush();
            }
        }

    } catch (
    Exception $e){
        echo 
    "There was an error with your request or the service is unavailable.\n";
        
    print_r ($e);
    }
    ?>
    Anyway i will be happy to talk with people about how to really make some profit out of my stuff or maybe code some new stuff :)

    If you have any problems with this script PM me.

    Thanks,
    kod
     
  2. dcoder

    dcoder Newbie

    Joined:
    Dec 14, 2008
    Messages:
    1
    Likes Received:
    0
    How would you do this script to where it would recursively get all the results for a specific keyword upto the maximum of 10,000 results?

    -dc