cancel
Showing results for 
Search instead for 
Did you mean: 

Communicate with exteral Serp system

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Communicate with exteral Serp system

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

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Communicate with exteral Serp system

@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!

View solution in original post

6 REPLIES 6

Re: Communicate with exteral Serp system

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!

Re: Communicate with exteral Serp system

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?

Re: Communicate with exteral Serp system

HI @kevin_marshall2,

 

What adjuctly you want to do? Then I can suggest better. 

Do you want to use Magento Rest API?

Re: Communicate with exteral Serp system

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.

Re: Communicate with exteral Serp system

@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!

Re: Communicate with exteral stock system

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