使用 PHP sdk v2 从我们的商家中心获取数据
Fetch data from our merchant center using the PHP sdk v2
举个例子,比如获取产品信息或获取类别。我正在使用这段代码。因为我是 运行 这段代码,所以我没有得到任何类别。请提供获取数据的任何示例
<?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);**
最后必须执行请求以检索类别。
print_r($request->execute());
在执行命令之前,请求对象仍然是 RequestBuilder,因为可以向 Get-Request 添加额外的参数。在下面的完整示例中,ML 和 Import API 客户端的配置已被调整,因为它们使用不同的 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());
举个例子,比如获取产品信息或获取类别。我正在使用这段代码。因为我是 运行 这段代码,所以我没有得到任何类别。请提供获取数据的任何示例
<?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);**
最后必须执行请求以检索类别。
print_r($request->execute());
在执行命令之前,请求对象仍然是 RequestBuilder,因为可以向 Get-Request 添加额外的参数。在下面的完整示例中,ML 和 Import API 客户端的配置已被调整,因为它们使用不同的 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());