Here I have tried but I getting this error:

Fatal error: Uncaught SoapFault exception: [env:Sender] %fieldName is a required field.

require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = '6ge34qaj7ugklona2in8fmmig93qmblx';
$wsdlUrl = 'http://localhost/magento214/soap/default?wsdl&services=catalogProductRepositoryV1';
$opts = ['http'=> ['header' => "Authorization: Bearer " .$token]];
$context = stream_context_create($opts);
$soapClient  = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
      $productData = array(
        'sku'               => 'ZZ-TEST',
        'name'              => 'testproduct',
        'visibility'        => 4, /*'catalog',*/
        'type_id'           => 'simple',
        'price'             => 45,
        'status'            => 1,
        'attribute_set_id'  => 4,
        'weight'            => 1,
        'custom_attributes' => array(
            array( 'attribute_code' => 'category_ids', 'value' => ['3'] ),
            array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
            array( 'attribute_code' => 'short_description', 'value' => 'Simple  Short Description' ),
            )
    );
      $productData = json_encode(array('product' => $productData));
     //  print_r($productData);exit;
$result = $soapClient->catalogProductRepositoryV1save($productData);
var_dump($result);
  • By posting only array of product data it is showing me Invalid product data: %1 changes i made is '$result = $soapClient->catalogProductRepositoryV1save(array('product' => $productData));' and removed the previous line'. – Bibhu Asish Mar 16 '17 at 7:17
up vote 5 down vote accepted

The code below works.

Tested on Magento 2.1.4.

The error you received:

Fatal error: Uncaught SoapFault exception: [env:Sender] %fieldName is a required field.

was due to json_encodeing $productData. Magento simply could not find product fieldName.

You don't need to json_encode things and parameters should be typeId and attributeSetId.

require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'qdst8d40dfdfdfdfd43r3ererenx26f';
$wsdlUrl = 'http://m214.domain.com/soap/default?wsdl&services=catalogProductRepositoryV1';
$opts = ['http'=> ['header' => "Authorization: Bearer " .$token]];
$context = stream_context_create($opts);
$soapClient  = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
  $productData = array(
    'sku'               => 'ZZ-TEST',
    'name'              => 'testproduct',
    'visibility'        => 4,
    'typeId'           => 'simple',
    'price'             => 45,
    'status'            => 1,
    'attributeSetId'  => '4',
    'weight'            => 1,
);
$result = $soapClient->catalogProductRepositoryV1save(array('product' => $productData));
var_dump($result);

Disclaimer: I could not make description, category_ids and short_description work though.

  • thanks @konstantin, I have removed the json_encode and tried this earlier but not checked with typeId & attributeSetId.It worked thnx. – Bibhu Asish Apr 7 '17 at 7:06
  • very welcome sir! – Konstantin Gerasimov Apr 7 '17 at 16:11
  • @KonstantinGerasimov How do I send 'qty'=>'20.0000' please? – Juliano Vargas Feb 19 at 20:10
  • How do I update the price and stocks of the products using the values in another database? @KonstantinGerasimov – Usman Yousaf Oct 25 at 10:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.