0

how can i select all products and one product by his SKU? Line 23.

$serviceArgs = ["sku" => 'SKU'];

function getAuthToken($hostname, $username, $password)
    {
      $wsdlUrl = "http://$hostname/soap/default?wsdl=1&services=integrationAdminTokenServiceV1";
      $soapClient = new SoapClient($wsdlUrl, ['soap_version' => SOAP_1_2]);
      $serviceArgs = ['username' => $username, 'password' => $password];
      $response = $soapClient->integrationAdminTokenServiceV1CreateAdminAccessToken($serviceArgs);
      return $response->result;
    }

    $token = getAuthToken($hostname, $username, $password);
    $opts = [
      'http'=> [
          'header' => 'Authorization: Bearer ' . $token
      ]
    ];
    $wsdlUrl = "http://$hostname/soap/default?wsdl=1&services=catalogProductRepositoryV1";

    $serviceArgs = ["sku" => 'SKU'];

    $context = stream_context_create($opts);

    $soapClient = new SoapClient($wsdlUrl, array(
      'soap_version' => SOAP_1_2,
      'stream_context' => $context
    ));

    $response = $soapClient->catalogProductRepositoryV1Get($serviceArgs);

    print_r ($response);

1 Answer 1

1

Try this

$tokenUrl = 'https://example.com/soap/default?wsdl&services=integrationAdminTokenServiceV1';
$catalogProductRepositoryUrl = 'https://example.com/soap/default?wsdl&services=catalogProductRepositoryV1';

$username = 'MyUsername';
$password = 'MyPassword';
$options = [
    'soap_version' => SOAP_1_2,
    'trace' => 1,
    'connection_timeout' => 120,
];

$soapClient = new SoapClient($tokenUrl, $options);
$response = $soapClient->integrationAdminTokenServiceV1CreateAdminAccessToken([
    'username' => $username,
    'password' => $password
]);

$token = $response->result;

// create bearer token Authorization header
$options['stream_context'] = stream_context_create([
    'http' => [
        'header' => sprintf('Authorization: Bearer %s', $token)
    ]
]);

// get product with sku 24-MB04
$soapClient = new SoapClient($catalogProductRepositoryUrl, $options);
$response = $soapClient->catalogProductRepositoryV1Get(['sku' => '24-MB04']);
$result = $response->result;
$productName = $result->name; //Strive Shoulder Pack


// get all products or use filters on collection
$searchCriteria = [
    'searchCriteria' => [
        'filterGroups' => [
            [
                /*'filters' => [
                    [
                        'field' => 'sku',
                        'value' => '24-MB04',
                        'condition_type' => 'eq',
                    ],
                ],*/
            ],
        ],
        //'current_page' => 1,
        //'page_size' => 2,
    ],
];

$soapClient = new SoapClient($catalogProductRepositoryUrl, $options);
$response = $soapClient->catalogProductRepositoryV1GetList($searchCriteria);
$result = $response->result;
$items = $result->items;
$total = $result->totalCount;

You can also check dev/tests/api-functional/testsuite files for more examples

UPD: and example how to get all products

$soapTokenUri = 'https://example.com/soap/default?wsdl&services=integrationAdminTokenServiceV1';
$catalogProductRepositoryUrl = 'https://example.com/soap/default?wsdl&services=catalogProductRepositoryV1';

$username = 'MyUsername';
$password = 'MyPassword';
$options = [
    'soap_version' => SOAP_1_2,
    'trace' => 1,
    'connection_timeout' => 120,
];

$soapClient = new SoapClient($soapTokenUri, $options);
$response = $soapClient->integrationAdminTokenServiceV1CreateAdminAccessToken([
    'username' => $username,
    'password' => $password
]);

$token = $response->result;

// create bearer token Authorization header
$options['stream_context'] = stream_context_create([
    'http' => [
        'header' => 'Authorization: Bearer ' . $token
    ]
]);

// get all products or use filters on collection
$searchCriteria = [
    'searchCriteria' => [
        'filterGroups' => [[]],
    ],
];

$soapClient = new SoapClient($catalogProductRepositoryUrl, $options);
$response = $soapClient->catalogProductRepositoryV1GetList($searchCriteria);
$result = $response->result;
$items = $result->items;
$total = $result->totalCount;
var_dump($items);
4
  • Sorry, I not uderstand code. How can i select all products?
    – Alessandro
    Oct 2, 2017 at 9:22
  • with catalogProductRepositoryV1GetList method and searchCriteria with empty filterGroups. Try last block from my example Oct 2, 2017 at 9:26
  • Fatal error: Uncaught SoapFault exception: [Client] SoapClient::SoapClient(): 'uri' option is required in nonWSDL mode in /public_html/folder/file.php:89 Stack trace: #0 /public_html/folder/file.php(89): SoapClient->SoapClient(NULL, Array) #1 {main} thrown in /public_html/folder/file.php on line 89
    – Alessandro
    Oct 2, 2017 at 9:39
  • updated the answer Oct 2, 2017 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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