我目前正在创建一个网络应用程序,需要循环访问多个 BigCommerce 商店。不幸的是,使用 BigCommerce API 时我无法让它循环。
我正在使用 GitHub 上最新版本的 BC API PHP 库(使用命名空间的库),请参阅下面的代码:
require_once( 'autoload.php' );
use Bigcommerce\Api\Client as Bigcommerce;
$stores[1]['url'] = 'https://www.store1.co.uk';
$stores[1]['api_key'] = 'e01e16e6b51d70f6de213fd7445dc0f4';
$stores[1]['user'] = 'admin';
$stores[2]['url'] = 'https://www.store2.co.uk';
$stores[2]['api_key'] = '7b8b934e157eac734b7f7b4311b7cd81';
$stores[2]['user'] = 'admin';
foreach ( $stores as $store ){
echo $store['url'] . ' - ';
Bigcommerce::configure(array(
'store_url' => $store['url'],
'username' => $store['user'],
'api_key' => $store['api_key'],
));
Bigcommerce::setCipher('RC4-SHA');
Bigcommerce::verifyPeer( false );
$products = Bigcommerce::getProductsCount();
echo $products . ' products<br />;
}
预期输出应该是:
https://www.store1.co.uk - 301 products
https://www.store2.co.uk - 235 products
我实际得到的是:
https://www.store1.co.uk - 301 products
https://www.store2.co.uk -
我之前曾使用过该 API 几次,但每次/每个项目只连接到一个商店。在连接到 foreach 循环中的下一个商店之前,是否需要关闭连接或其他操作?
非常感谢所有帮助!
最佳答案
查看 Bigcommerce PHP 库中的以下代码段。
/**
* Get an instance of the HTTP connection object. Initializes
* the connection if it is not already active.
*
* @return Connection
*/
private static function connection()
{
if (!self::$connection) {
self::$connection = new Connection();
self::$connection->authenticate(self::$username, self::$api_key);
}
return self::$connection;
}
这来自 Client.php - https://github.com/bigcommerce/bigcommerce-api-php/blob/master/src/Bigcommerce/Api/Client.php
如果你看看你正在做什么,它不会工作,因为现有的连接将迫使库忽略新值。仅当您第一次设置时才会形成新的商店连接。
针对您的特殊用例的一个简单修复方法是覆盖上述函数以重新连接到新商店,而不管现有连接如何。
希望这能成功!
关于php - BigCommerce api循环中的多个商店,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15507655/