2

我正在尝试使用 google content api 使用 php 进行购物。谷歌文档为此提供了以下代码。但最后一行 $service 使用但未定义。

 $product = new Google_Service_ShoppingContent_Product();
$product->setOfferId('book123');
$product->setTitle('A Tale of Two Cities');
$product->setDescription('A classic novel about the French Revolution');
$product->setLink('http://my-book-shop.com/tale-of-two-cities.html');
$product->setImageLink('http://my-book-shop.com/tale-of-two-cities.jpg');
$product->setContentLanguage('en');
$product->setTargetCountry('GB');
$product->setChannel('online');
$product->setAvailability('in stock');
$product->setCondition('new');
$product->setGoogleProductCategory('Media > Books');
$product->setGtin('9780007350896');

$price = new Google_Service_ShoppingContent_Price();
$price->setValue('2.50');
$price->setCurrency('GBP');

$shipping_price = new Google_Service_ShoppingContent_Price();
$shipping_price->setValue('0.99');
$shipping_price->setCurrency('GBP');

$shipping = new Google_Service_ShoppingContent_ProductShipping();
$shipping->setPrice($shipping_price);
$shipping->setCountry('GB');
$shipping->setService('Standard shipping');

$shipping_weight = new Google_Service_ShoppingContent_ProductShippingWeight();
$shipping_weight->setValue(200);
$shipping_weight->setUnit('grams');

$product->setPrice($price);
$product->setShipping(array($shipping));
$product->setShippingWeight($shipping_weight);

$result = $service->products->insert($merchant_id, $product);
4

1 回答 1

2

创建服务实例。

// include your composer dependencies
require_once 'vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=Merchant-Center-XXXXXx.json');

$client = new Google_Client();
$client->useApplicationDefaultCredentials();

$client->addScope('https://www.googleapis.com/auth/content');

$service = new Google_Service_ShoppingContent($client);
于 2019-09-20T05:33:04.513 回答