Scroll Top

Tutorials

Admin REST API

ADMIN REST API

DEMO AND DOCUMENTATION SITES
Shopping Cart API

SHOPPING CART REST API

DEMO AND DOCUMENTATION SITES

How to install REST API extension

How to: Checkout steps tutorials

PHP SAMPLE CODES

Working PHP sample code included in the extension zip file.
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 upload Product additional  image:
$siteUrl = 'https://opencart3-oauth.api.opencart-api.com/';
$bearerToken = "5db353e4201e8f5ff02ade4a74f4d920c38fe959";

$imagePath = 'c:\Users\xxx\Downloads\product_1_upgrade.png';

$productId = 34;

$postFields['file'] = new CurlFile($imagePath, mime_content_type($imagePath));

$ch = curl_init($siteUrl.'index.php?route=rest/product_admin/productimages&id='.$productId.'&other=1&sort_order=1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$bearerToken
));
$data = curl_exec($ch);

$response = json_decode($data, true);
$info = curl_getinfo($ch);
curl_close($ch);
Sample PHP code to upload option image:
$siteUrl = 'http://opencart3-oauth.api.opencart-api.com/';
$bearerToken = "YOUR_BEARER_TOKEN";
$imagePath = 'C:/your_folder/android.jpg';

$optionValueId = 39; //it is just an example, you have to change it

$postFields['file'] = new CurlFile($imagePath, mime_content_type($imagePath));

$ch = curl_init($siteUrl.'index.php?route=rest/option_admin/optionimages&id='.$optionValueId.'&access_token='.$bearerToken);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

$data = curl_exec($ch);

$response = json_decode($data, true);

$info = curl_getinfo($ch);
curl_close($ch);
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);
}