Tutorials

How to: Checkout steps tutorials

PHP sample codes

Sample PHP code to get products:

If your secret key is : 123
$url	= "http://webshop.opencart-api.com:80/api/rest/products";
$secret = "123"; 

$ch = curl_init($url);
		
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"X-Oc-Merchant-Id: ".$secret
));

$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($status != 200) {
echo("ERROR");
}else {
echo($response);
}
Sample PHP code to add product:
$product = array(
	"model"=> "test_model",
	"sku"=> "001",
	"quantity" => "10",
	"price"=> "1000",
	"tax_class_id"=> "9",
	"manufacturer_id"=> "8",
	"sort_order"=> "1",
	"status"=> "1",
	"product_store"=> array(0),
	"product_category"=> array(71),
	"product_description"=>array(
		"1"=>array(
			"name"=>"Api test product",
			"meta_description" => "test product meta_description",
			"meta_keyword" => "test product meta_keyword",
			"description" => "test product description"
			)
		)
	
);
$secret = "123";
$data_string = json_encode($product);                                                                                   
$ch = curl_init('http://webshop.opencart-api.com:80/api/rest/products');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");   
curl_setopt($ch, CURLOPT_HEADER, 0);                                                                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',     
    'X-Oc-Merchant-Id: '.$secret,                                                                           
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                           
 
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status != 200) {
	echo("ERROR");
}else {
	echo($response);
}

Sample PHP code to upload and set product’s primary image:
$imagePath = '/home/your_folder/Pictures/camera-icon.png';
$imageExtention = preg_replace('/^.*\.([^.]+)$/D', '$1', $imagePath);
$postFields['file'] = "@$imagePath;type=image/$imageExtention";

$secret = "123";                                                                           

$ch = curl_init('http://webshop.opencart-api.com:80/api/rest/products/35/images');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");   
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);                                                                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'X-Oc-Merchant-Id: '.$secret                                                                    
));                                                                                                           
 
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status != 200) {
	echo("ERROR");
}else {
	echo($response);
}
Sample PHP code to get OAuth token:
$ch = curl_init('http://opencartoauth.opencart-api.com:80/api/rest/oauth2/token/client_credentials');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Basic ZGVtb19vYXV0aF9jbGllbnQ6ZGVtb19vYXV0aF9zZWNyZXQ='
    )
);

$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status != 200) {
    echo("ERROR");
}else {
    echo($response);
}

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close