phpcommercetools

Fetch data from our merchant center using the PHP sdk v2


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);**


Solution

  • 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());