Tutorials

How to: Checkout steps tutorials

PHP sample codes

Working PHP sample code included in the package

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 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);
}