0

I'm using this code to update my Magento 2 stocks via API; but there is an permission error, even the credentials are ok, and user role is 'administrator'.

Could some one help me?

ERROR Message:

string(366) " Forbidden You don't have permission to access /rest/V1/products/10001001/stockItems/1 on this server. Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

And my code:

<?php
$url = "https://www.example.com/";
$token_url=$url."rest/V1/integration/admin/token";
$ch = curl_init();
$data = array("username" => "xxxx", "password" => "xxxx");
$data_string = json_encode($data);
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$adminToken=  json_decode($token);

$skus = array(
  '10001001' => 10,
  '10001004' => 8
);

foreach ($skus as $sku => $stock) {
  $requestUrl='https://www.example.com/rest/V1/products/' . $sku . '/stockItems/1';
  $sampleProductData = array(
          'qty' => $stock
  );

$productData = json_encode(array('product' => $sampleProductData));
$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

  var_dump($response);
}
?>
Pradeep
  • 9,667
  • 13
  • 27
  • 34
stackers_alti
  • 93
  • 1
  • 12
  • quite simply, you don't have correct permissions. Make sure files are 644 and directories are 755. Make sure the owner/group is also correct (if you own example.com) or, pass it in user/password (if it needs be) – treyBake May 24 '18 at 11:50
  • @ThisGuyHasTwoThumbs Thank you for your answer. But an another php file in the same folder (creates products via rest api; using same method and same credentials) is working? – stackers_alti May 24 '18 at 15:30
  • have you checked file permissions? (`ls -la`) – treyBake May 24 '18 at 15:33
  • @ThisGuyHasTwoThumbs i wrote 'chown -R me:me public_html' in ssh.. (means, all files in public_html owned by me).. also all files are 644, folders are 755 in public_html. – stackers_alti May 24 '18 at 15:45
  • I know what `chown -R` does :) and how about the actual file permissions? are the `644`? also just because you own it, doesn't mean your the user that executes the script, could be `apache`, could be `www-data` etc. – treyBake May 24 '18 at 15:46
  • by the way; ater changing 644-755 difference; im getting 404 error: string(619) " 404 error: Page not found. " (404 is for https://www.example.com/rest/V1/products/10001001/stockItems/1 i think) – stackers_alti May 24 '18 at 15:50
  • @ThisGuyHasTwoThumbs but that link is also working fine; when i wrote it on browser url; it returns Request does not match any route. – stackers_alti May 24 '18 at 15:58
  • Then the route is wrong. Error messages never lie :) – treyBake May 24 '18 at 16:39
  • i dont have any idea :( https://www.example.com/rest/V1/products/10001001/stockItems/1 should be true... it returns Request does not match any route. .. even i add /index.php/ or not.. – stackers_alti May 24 '18 at 16:51

0 Answers0