我目前正在创建一个需要在多个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循环中的下一个商店之前,是否需要关闭连接?
非常感谢所有人的帮助!
答案 0 :(得分:2)
查看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
如果你看看你在做什么,它将无法工作,因为现有连接将强制库忽略新值。只有在您第一次设置时才会形成新的商店连接。
对您的特殊用例的一个简单修复是覆盖上述功能,无论现有连接如何,都可以重新连接到新商店。
希望这会成功!