Post by cozzienz » Wed Jun 12, 2013 1:42 pm

Hi All,

I have received an API from a New Zealand Auction Site which can integrate with websites to retrieve information like product status's etc, however they do not know where the placement of the API code should be position in a opencart site

Any one know where this should go???
I have the api identifier and private key

Code that was provided is as follows

Code: Select all

<?php

// put products data into an array - we're using an array of objects here
$products=array();

$userid=3044; // your sitemember number
$apiid='your api identifier';
$privateKey='your private key';

$timestamp=time();

$xml='<?xml version="1.0" ?>';
$xml.="<lstImport>\n";
$xml.="<ident>\n";
$xml.="<version>1</version>\n";
$xml.="<timestamp>".$timestamp."</timestamp>\n";
$xml.="<user>".$userid."-".$apiid."</user>\n";
$xml.="</ident>\n";

foreach($products as $product)
{             
 
  // ****
  $lstcat=11957;
  // ****
         
  $price=myXMLEntities($product->product_price);
  $name=myXMLEntities($product->product_name);       
   
  $desc=myXMLEntities($product->product_description);      

  $photo=myXMLEntities($_SERVER['HTTP_HOST'].$product->product_image);
 
     $weblink=myXMLEntities('http://');    // provide a link to the product on your website

     // we're using a set of image URLs separated by the | character
  $imagearr=explode('|',$product->product_image);

  $uniqueid=myXMLEntities($product->product_id);

  $name=myXMLEntities($product->product_name);

  $price=myXMLEntities($product->product_price);
 
 
  // for XML generation
  $productstr='';
  $productstr.="<product>\n";
 
  // will be extended in version 2, for now we only support standard products
  $productstr.="<productType>standard</productType>\n";
  //$productstr.="<productType>rental-property</productType>\n";
  //$productstr.="<productType>lease-property</productType>\n";
  //$productstr.="<productType>retirement-property</productType>\n";
  //$productstr.="<productType>forsale-property</productType>\n";
  //$productstr.="<productType>vehicle-full</productType>\n"; 
  //$productstr.="<productType>vehicle-limited</productType>\n"; 
  //$productstr.="<productType>boat</productType>\n"; 
 
  // the unique id for your product
  $productstr.="<productUniqueID>".$uniqueid."</productUniqueID>\n";

  // the List Sell Trade category
  $productstr.="<productCategory>".$lstcat."</productCategory>\n";
 
  // your category identifier, e.g. Hats
  $productstr.="<productCategoryName>Hats</productCategoryName>\n";
 
  // product title
  $productstr.="<productTitle>".$name."</productTitle>\n";
 
  // product description
  $productstr.="<productDescription>".$desc."</productDescription>\n";
 
  // product link to website
  $productstr.="<productLink>".$weblink."</productLink>\n";

  // one of following two sets of options
  $productstr.="<productSaleType>selling-fixed</productSaleType>\n";
  $productstr.="<productBuyPrice>".$price."</productBuyPrice>\n";
  // or
  //$productstr.="<productSaleType>selling-fixed</productSaleType>\n";
  //$productstr.="<productBuyNowPrice>".$buynowprice."</productBuyNowPrice>\n";
  //$productstr.="<productStartPrice>".$startprice."</productStartPrice>\n";
  //$productstr.="<productReservePrice>".$reserveprice."</productReservePrice>\n";

  // one of the following three options
  $productstr.="<productPickup>can-pick-up</productPickup>\n";
  //$productstr.="<productPickup>no-pick-up</productPickup>\n"
  //$productstr.="<productPickup>must-pick-up</productPickup>\n"

  $productstr.="<productPaymentTypes>bank-deposit</productPaymentTypes>\n";

  //$productstr.="<productPaymentTypes>cash|credit-card|bank-deposit</productPaymentTypes>\n";
  $productstr.="<productShipping>tbc-shipping</productShipping>\n";
  $productstr.="<productShippingNote></productShippingNote>\n";

  // use 1 for current, 0 to remove
  $productstr.="<productCurrent>1</productCurrent>\n";

  // number of days to list for
  $productstr.="<productDuration>4</productDuration>\n";

  // up to 10 images - urls of images
  $productstr.="<productImages>\n";      
  foreach($imagearr as $image)
  {
    $imageurl=myXMLEntities('http://'.$_SERVER['HTTP_HOST']."/".$image);
    $productstr.="<image>".$imageurl."</image>\n";
  }      
  $productstr.="</productImages>\n";

  $productstr.="</product>\n";
     
  $xml.=$productstr;     
 
}

$xml.="</lstImport>\n";

// get the hash using private key
$hash=hash_hmac('sha256', $xml, $privateKey);

$output=str_ireplace('<lstImport>','<lstImport><hash>'.$hash."</hash>",$xml);

$url = 'https://listselltrade.co.nz/api/v1/upload.php';     // test

$data = $output;

$fields_string='data='.urlencode($data);   // must supply data!

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);
echo nl2br(htmlentities($result, ENT_NOQUOTES, "UTF-8"));

//close connection
curl_close($ch);
exit;

function myXMLEntities($string){
  return htmlentities($string, ENT_NOQUOTES, "UTF-8");
}

?>

User avatar
Active Member

Posts

Joined
Sun May 15, 2011 11:13 am

Post by welfordmedia » Wed Jun 12, 2013 6:58 pm

You can add a normal file at root level and access that way (i.e. where the index.php file is, add it in the same folder) - then just go to the file like: http://www.youstore.com/yourfile.php


By the looks of the code above its missing lots of work needed to map OpenCart data to it.

An awesome Web Development company based in Leeds. E-Commerce Specialists by day, Geeks and Gamers by night

Tel: 0845 634 1838
Web: http://www.welfordmedia.co.uk
Twitter: https://twitter.com/welfordmedia
Facebook: https://www.facebook.com/welfordmedia


User avatar
Active Member

Posts

Joined
Thu Jul 21, 2011 5:59 pm
Location - Leeds, UK
Who is online

Users browsing this forum: No registered users and 37 guests