I am very new to Magento 2 and I saw this Vishalsanwar86 on another post and would like to alter it to communicate with my Serp system.
What other files would be need, is this a controller?
Do I store these in the app/code section or somewhere else?
<?php
$adminUrl = 'http://localhost/magento2/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "admin"); //Admin Login details
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token= json_decode($token);
$headers = array("Authorization: Bearer $token","Content-Type: application/json");
$skus = array(
'100001' => 66,
'100002' => 99
);
//Here 100001 and 100002 are SKU and 66 and 99 are Qty
foreach ($skus as $sku => $stock) {
$requestUrl='http://localhost/magento2/rest/V1/products/' . $sku . '/stockItems/1';
$sampleProductData = array(
"qty" => $stock
);
$productData = json_encode(array('stockItem' => $sampleProductData));
$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, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
unset($productData);
unset($sampleProductData);
}
Solved! Go to Solution.
@kevin_marshall2
I got your question now.
The two urls in the post has there different mean:
$adminUrl = 'http://localhost/magento2/rest/V1/integration/admin/token/'; $ch = curl_init(); $data = array("username" => "admin", "password" => "admin"); //Admin Login details
It is used for authenticate the rest API user with magento, that the user have access to update in magento or not.
There are also a different method as well to authenticate with magento without admin user name and password in the code using Token. You can use this method as well.
for more info:
https://devdocs.magento.com/guides/v2.3/get-started/authentication/gs-authentication-token.html
$requestUrl='http://localhost/magento2/rest/V1/products/' . $sku . '/stockItems/1';
The above url is with inventory endpoint for update the inventory in the magento.
for more info:
https://devdocs.magento.com/swagger/#/catalogProductRepositoryV1/catalogProductRepositoryV1SavePut
The following url also may help you.
create-a-custom-rest-api-for-bulk-product-update-in-magento-2
I hope it will help you!
Hi @kevin_marshall2,
Code you mentioned looks like simple curl request.
You can keep it it in Model. Create a new model file under Model folder and keep in it in a function. Then you can call that function anywhere where you want.
app/code/NAMESPACE/MODULE_NAME/Model/FILENAME.php
You can create a new module for it.
To create a new module:
https://devdocs.magento.com/videos/fundamentals/create-a-new-module/
https://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/
I hope it will help you!
I am just a bit confused, there appears to be a call to 2 different URL's, there is a section at the top and another at the bottom but only the bottom seems to show a response?
HI @kevin_marshall2,
What adjuctly you want to do? Then I can suggest better.
Do you want to use Magento Rest API?
Sorry wrong term, it is not a serp but a stock control/sales system that we to sync Magento 2 with, it has an API I just need to 'POST' info in standard form format and then process the response adding the relevant info to Magento, most likely will be triggered by a cron job.
@kevin_marshall2
I got your question now.
The two urls in the post has there different mean:
$adminUrl = 'http://localhost/magento2/rest/V1/integration/admin/token/'; $ch = curl_init(); $data = array("username" => "admin", "password" => "admin"); //Admin Login details
It is used for authenticate the rest API user with magento, that the user have access to update in magento or not.
There are also a different method as well to authenticate with magento without admin user name and password in the code using Token. You can use this method as well.
for more info:
https://devdocs.magento.com/guides/v2.3/get-started/authentication/gs-authentication-token.html
$requestUrl='http://localhost/magento2/rest/V1/products/' . $sku . '/stockItems/1';
The above url is with inventory endpoint for update the inventory in the magento.
for more info:
https://devdocs.magento.com/swagger/#/catalogProductRepositoryV1/catalogProductRepositoryV1SavePut
The following url also may help you.
create-a-custom-rest-api-for-bulk-product-update-in-magento-2
I hope it will help you!
So if I need to encode this in magento curl
http://<my system address>/?script=WebsiteSQL&tablename=ITEMMASTER&format=json&keyfield=ITEMCODE&condition=ITEMCODE=’313-6F7’&apikey=<key>
I would use the following? but how do I send it Options?
$adminUrl = "<my system address>';
$ch = curl_init();
$data = array("script" => "WebsiteSQL" , '"tablename" => "ITEMMASTER" , "format" => "json" , "keyfield" => "ITEMCODE" , "condition" => "ITEMCODE='313-6f7'" , "apikey" => "<key>");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); //or POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'); // Add the rest only if using POST ,'Content-Length: ' . strlen($data_string)));
$token = curl_exec($ch);
$token= json_decode($token);