1

Just give an example like fetching product info or fetching categories. I am using this code. As I am running this code I am not getting any categories. Please provide any example of fetching the data

<?php
namespace Commercetools;

use Commercetools\Api\Client\ClientCredentialsConfig;
use Commercetools\Api\Client\Config;
use Commercetools\Client\ClientCredentials;
use Commercetools\Client\ClientFactory;

require_once __DIR__ . '/vendor/autoload.php';

/** @var string $clientId */
/** @var string $clientSecret */
$clientId = "";
$clientSecret="";
$authConfig = new ClientCredentialsConfig(new ClientCredentials($clientId, $clientSecret));

$client = ClientFactory::of()->createGuzzleClient(
    new Config(),
    $authConfig
);
use Commercetools\Client\ApiRequestBuilder;
use Commercetools\Client\ImportRequestBuilder;
use Commercetools\Client\MLRequestBuilder;
use GuzzleHttp\ClientInterface;

/** @var ClientInterface $client */
$builder =  new ApiRequestBuilder('project-key', $client);
$request = $builder->with()->categories()->get();

$importBuilder =  new ImportRequestBuilder('project-key', $client);
$request = $importBuilder->with()->importSinks()->get();

$mlBuilder =  new MLRequestBuilder('project-key', $client);
$request = $mlBuilder->with()->recommendations()->generalCategories()->get();
**echo "<pre>";
print_r($request);**
James Z
  • 12,209
  • 10
  • 24
  • 44
Deep
  • 13
  • 7

1 Answers1

0

The request has to be executed finally to retrieve the categories.

print_r($request->execute());

Before the execute command the request object is still a RequestBuilder as it's possible to add additional parameters to the Get-Request. In the full example below the configuration for the ML and Import API client has been adjusted as they use a different API Url

<?php
namespace Commercetools;

require_once __DIR__ . '/vendor/autoload.php';

use Commercetools\Api\Client\ClientCredentialsConfig;
use Commercetools\Api\Client\Config;
use Commercetools\Client\ClientCredentials;
use Commercetools\Client\ClientFactory;

/** @var string $clientId */
/** @var string $clientSecret */
$clientId = "";
$clientSecret="";
$projectKey = "";
$authConfig = new ClientCredentialsConfig(new ClientCredentials($clientId, $clientSecret));

$apiClient = ClientFactory::of()->createGuzzleClient(
    new Config(),
    $authConfig
);
use Commercetools\Client\ApiRequestBuilder;
use Commercetools\Client\ImportRequestBuilder;
use Commercetools\Client\MLRequestBuilder;
use GuzzleHttp\ClientInterface;

/** @var ClientInterface $client */
$builder =  new ApiRequestBuilder($projectKey, $apiClient);
$request = $builder->with()->categories()->get();

//$importClient = ClientFactory::of()->createGuzzleClient(
//    new Import\Client\Config(),
//    $authConfig
//);
//$importBuilder =  new ImportRequestBuilder($projectKey, $importClient);
//$request = $importBuilder->with()->importSinks()->get();
//
//$mlClient = ClientFactory::of()->createGuzzleClient(
//    new Ml\Client\Config(),
//    $authConfig
//);
//
//$mlBuilder =  new MLRequestBuilder($projectKey, $mlClient);
//$request = $mlBuilder->with()->recommendations()->generalCategories()->get()->withProductName("test");

echo "<pre>";
print_r($request->execute());
jenschude
  • 292
  • 1
  • 6
  • Really thanks for your comment. Just getting this error plz check if you can and again thanks for the response. Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://auth.europe-west1.gcp.commercetools.com/oauth/token` resulted in a `401 Unauthorized` response: – Deep Aug 16 '21 at 12:10
  • Please double check that your credentials are correct. Also it may be, that your credentials had been invalidated as the initial example included them. – jenschude Aug 16 '21 at 12:30
  • Even created the new client api and changed the data still error is same – Deep Aug 16 '21 at 12:33
  • Do we need to add any .env file in the cloned folder? – Deep Aug 16 '21 at 12:38
  • I just retried it. When filling in the projectKey, clientId and clientSecret the above example works. There is no env file needed – jenschude Aug 16 '21 at 12:43