使用 Bigcommerce API 过滤和迭代 stdclass 对象数组
Filter & iterating through stdclass object array with Bigcommerce API
我正在尝试使用 Big commerce API 并根据我需要的任何参数过滤 json 响应。理想情况下,我想过滤地址在澳大利亚悉尼且运输状态为 "awaiting pickup" 的客户。这是我写的开头:
<?php
//Bigcommerce API credentials
$username = 'xxxxxx';
$api = 'xxxxxxxxxxxxxxxxxxxxxx';
//Curl requests
// Assign filters to variables
$city_name = 'Sydney';
$country_name = 'Australia';
$items_shipped = 0;
$customer_url = "https://store-xxxxxx.mybigcommerce.com/api/v2/customers.json";
// intiate curl request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $customer_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
// execute curl requests
$curlData = curl_exec($curl);
// Parse requests in Json Format
$product_rec = json_decode($curlData);
// returns a specific variable of StdClass Object, here the first name of obj[1]
echo $product_rec[0]->first_name;
// !!Here I'd like to implement an iteration othe reach std class objects and filter!!
// print the requests in Json format on screen
echo '<pre>';
print_r($product_rec);
// Close curl connexions
curl_close($curl);
?>
现在我得到这个答案:
Hyacinthe
Array
(
[0] => stdClass Object
(
[id] => 1
[company] =>
[first_name] => Hyacinthe
[last_name] => Hamon
[email] => yayahappy@yahoo.com
[phone] => 0671089566
[date_created] => Fri, 26 Feb 2016 05:19:27 +0000
[date_modified] => Fri, 26 Feb 2016 05:19:27 +0000
[store_credit] => 0.0000
[registration_ip_address] => 138.25.2.134
[customer_group_id] => 0
[notes] =>
[tax_exempt_category] =>
[reset_pass_on_login] =>
[addresses] => stdClass Object
(
[url] => https://store-xxxxxx.mybigcommerce.com/api/v2/customers/1/addresses.json
[resource] => /customers/1/addresses
)
[form_fields] =>
)
[1] => stdClass Object
(
[id] => 2
[company] => Zip
[first_name] => John
[last_name] => Lennon
[email] => john@beattles.com
[phone] =>
[date_created] => Sat, 27 Feb 2016 01:31:19 +0000
[date_modified] => Sat, 27 Feb 2016 01:31:19 +0000
[store_credit] => 0.0000
[registration_ip_address] => 114.75.66.199
[customer_group_id] => 0
[notes] =>
[tax_exempt_category] =>
[reset_pass_on_login] =>
[addresses] => stdClass Object
(
[url] => https://store-xxxxxx.mybigcommerce.com/api/v2/customers/2/addresses.json
[resource] => /customers/2/addresses
)
[form_fields] =>
)
)
我现在想用变量过滤结果。我想我需要做一个 foreach 循环,但我不确定该怎么做。有人可以帮忙吗?
我实际上会从订单对象开始。这将提供账单和送货地址以及订单状态 ID。可以筛选订单状态 ID,然后您可以使用剩余的结果筛选澳大利亚悉尼。
https://developer.bigcommerce.com/api/stores/v2/orders
/api/v2/orders?status_id={value}
我不确定您为什么要尝试先浏览客户资源。如果您担心检查现有客户帐户,可以查找值不是 0 的客户 ID。
我正在尝试使用 Big commerce API 并根据我需要的任何参数过滤 json 响应。理想情况下,我想过滤地址在澳大利亚悉尼且运输状态为 "awaiting pickup" 的客户。这是我写的开头:
<?php
//Bigcommerce API credentials
$username = 'xxxxxx';
$api = 'xxxxxxxxxxxxxxxxxxxxxx';
//Curl requests
// Assign filters to variables
$city_name = 'Sydney';
$country_name = 'Australia';
$items_shipped = 0;
$customer_url = "https://store-xxxxxx.mybigcommerce.com/api/v2/customers.json";
// intiate curl request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $customer_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
// execute curl requests
$curlData = curl_exec($curl);
// Parse requests in Json Format
$product_rec = json_decode($curlData);
// returns a specific variable of StdClass Object, here the first name of obj[1]
echo $product_rec[0]->first_name;
// !!Here I'd like to implement an iteration othe reach std class objects and filter!!
// print the requests in Json format on screen
echo '<pre>';
print_r($product_rec);
// Close curl connexions
curl_close($curl);
?>
现在我得到这个答案:
Hyacinthe
Array
(
[0] => stdClass Object
(
[id] => 1
[company] =>
[first_name] => Hyacinthe
[last_name] => Hamon
[email] => yayahappy@yahoo.com
[phone] => 0671089566
[date_created] => Fri, 26 Feb 2016 05:19:27 +0000
[date_modified] => Fri, 26 Feb 2016 05:19:27 +0000
[store_credit] => 0.0000
[registration_ip_address] => 138.25.2.134
[customer_group_id] => 0
[notes] =>
[tax_exempt_category] =>
[reset_pass_on_login] =>
[addresses] => stdClass Object
(
[url] => https://store-xxxxxx.mybigcommerce.com/api/v2/customers/1/addresses.json
[resource] => /customers/1/addresses
)
[form_fields] =>
)
[1] => stdClass Object
(
[id] => 2
[company] => Zip
[first_name] => John
[last_name] => Lennon
[email] => john@beattles.com
[phone] =>
[date_created] => Sat, 27 Feb 2016 01:31:19 +0000
[date_modified] => Sat, 27 Feb 2016 01:31:19 +0000
[store_credit] => 0.0000
[registration_ip_address] => 114.75.66.199
[customer_group_id] => 0
[notes] =>
[tax_exempt_category] =>
[reset_pass_on_login] =>
[addresses] => stdClass Object
(
[url] => https://store-xxxxxx.mybigcommerce.com/api/v2/customers/2/addresses.json
[resource] => /customers/2/addresses
)
[form_fields] =>
)
)
我现在想用变量过滤结果。我想我需要做一个 foreach 循环,但我不确定该怎么做。有人可以帮忙吗?
我实际上会从订单对象开始。这将提供账单和送货地址以及订单状态 ID。可以筛选订单状态 ID,然后您可以使用剩余的结果筛选澳大利亚悉尼。 https://developer.bigcommerce.com/api/stores/v2/orders
/api/v2/orders?status_id={value}
我不确定您为什么要尝试先浏览客户资源。如果您担心检查现有客户帐户,可以查找值不是 0 的客户 ID。