5

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);
1
  • 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'. Mar 16, 2017 at 7:17

1 Answer 1

4

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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