cancel
Showing results for 
Search instead for 
Did you mean: 

API magento from external PHP script

API magento from external PHP script

Hello everyone,

 

I need to insert some products in magento 2 using REST API from a PHP script. I'm thinking to use the cURL to connect to my magento server. Reading the webapi.xml i found /V1/products/:sku. Now...in my PHP code how do i write the piece ":sku"?

Like this "$ch = curl_init("http://magento.m2/index.php/rest/V1/products/{sku}")"?
But if I have to insert more than one product i can put this in a cycle? Or how?

 

thanks

3 REPLIES

Re: API magento from external PHP script

Yes you need to call create product rest api of magento 2 in a loop(cycle).

 

for example,lets say you have id from 1 to 10 and you would like to create 10 products of magento 2 using rest api.

 

First you need to create a loop and call function which creates products

 

Below is the example  code for loop(cycle) :

 

 

$id = 1;
for($id=1;$id<=10;$id++)
{
$result = $this->createm2product();
}

 

Below is your createm2product function :

public function createm2product()
{
$magento2url = "http://your_m2_url/index.php/rest/V1/products/";
    $m2accesstoken = "2qamuatacwclnfgqd5856awjo7adhe42"; // here you need to pass access token
    $params = array("product"=>$param_final = array(
        'sku'               => 'your_product_sku',
        'name'              => 'your_product_name',
        'type_id'           => 'your_product_type_id', //here you need to pass product type id
        'price'             => 120,
        'attribute_set_id'  => 4,
        'visibility'         => 'your_visiblity_id', // here you need to pass your visibility id ex - 4(catalog,search)
        'extension_attributes' => array(
            'stockItem' => array(
                'qty' => 12,
				)
			),
		)
    );    

$methodname = "POST";
$response = executeRest($magento2url,$params,$m2accesstoken,$methodname);
$response_final = json_decode($response,true);
return $response_final;	

}

Below is the function code for curl request :

function executeRest($requestURL,$params,$adminToken,$methodname){
    $data_string = json_encode($params); 
    $curl = curl_init($requestURL);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST,$methodname);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($curl, CURLOPT_HTTPHEADER,array(
        'Content-Type:application/json',
        'Authorization:Bearer '.$adminToken
    ));
    $response = curl_exec($curl); 

    curl_close($curl);
    return $response;
}

For more reference and to know about parameters you can refer this link - http://devdocs.magento.com/swagger/

 

if issue solved,Click Kudos & Accept as Solution

Re: API magento from external PHP script

Hi @ManthanDave,

 

thank you for your answer. I have tried it but doen't work. No errors returns but It doesn't create any product in magento 

Re: API magento from external PHP script

If product is not created , then error is there in error log file.

 

so kindly check error log file and post the error log for the same.

 

also before using script , please check with postman application , call api from their first , if product is created from there or not ?

if issue solved,Click Kudos & Accept as Solution