【发布时间】:2015-10-24 02:43:11
【问题描述】:
您好,我使用 woocommerce rest API 访问 woocommerce 网站上的订单信息。
数据以 json 格式返回,我可以通过这个循环访问所有数据:
$orders = $connect->orders->get();
$order_numbers = $orders->orders;
foreach ($order_numbers as $order_number) {
$order_data = $connect->orders->get($order_number->order_number);
echo '</br>' . $order_number->order_number . '</br>';
echo $order_data->order->total . '</br>';
$line_items = $order_data->order->line_items;
foreach($line_items as $item){
echo $item->name . ' x ' . $item->quantity . '</br>';
}
}
第一行是获取订单列表的 API 调用。
这允许我在不指定 order_number 的情况下从第一个对象输出数据。但是,当访问 stdObject 中的数组时,例如“line_items”,我必须定义一个订单号。为了克服这个问题,我这次在 foreach 内部进行了另一个 api 调用,指定了访问信息的订单号。
$order_data = $connect->orders->get($order_number->order_number);
问题在于第二个 API 调用大大减慢了整个页面的速度。
有没有办法让我将其减少为单个 api 调用以加快页面渲染速度。
作为参考,这是我正在访问的数据:
object(stdClass)[15]
public 'order' =>
object(stdClass)[16]
public 'id' => int 22
public 'order_number' => int 22
public 'created_at' => string '2015-07-30T14:01:54Z' (length=20)
public 'updated_at' => string '2015-07-30T14:01:54Z' (length=20)
public 'completed_at' => string '2015-07-30T13:01:54Z' (length=20)
public 'status' => string 'on-hold' (length=7)
public 'currency' => string 'GBP' (length=3)
public 'total' => string '3.84' (length=4)
public 'subtotal' => string '3.84' (length=4)
public 'total_line_items_quantity' => int 2
public 'total_tax' => string '0.00' (length=4)
public 'total_shipping' => string '0.00' (length=4)
public 'cart_tax' => string '0.00' (length=4)
public 'shipping_tax' => string '0.00' (length=4)
public 'total_discount' => string '0.00' (length=4)
public 'shipping_methods' => string '' (length=0)
public 'payment_details' =>
object(stdClass)[17]
public 'method_id' => string 'bacs' (length=4)
public 'method_title' => string 'Direct Bank Transfer' (length=20)
public 'paid' => boolean false
public 'billing_address' =>
object(stdClass)[18]
public 'first_name' => string 'Chris' (length=5)
public 'last_name' => string '#' (length=5)
public 'company' => string '' (length=0)
public 'address_1' => string '#' (length=4)
public 'address_2' => string '' (length=0)
public 'city' => string '#' (length=7)
public 'state' => string '' (length=0)
public 'postcode' => string '#' (length=7)
public 'country' => string 'GB' (length=2)
public 'email' => string '#' (length=20)
public 'phone' => string '#' (length=11)
public 'shipping_address' =>
object(stdClass)[19]
public 'first_name' => string 'Chris' (length=5)
public 'last_name' => string '#' (length=5)
public 'company' => string '' (length=0)
public 'address_1' => string '#' (length=4)
public 'address_2' => string '' (length=0)
public 'city' => string '#' (length=7)
public 'state' => string '' (length=0)
public 'postcode' => string '#' (length=7)
public 'country' => string 'GB' (length=2)
public 'note' => string '' (length=0)
public 'customer_ip' => string '#' (length=15)
public 'customer_user_agent' => string 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4' (length=134)
public 'customer_id' => int 1
public 'view_order_url' => string '#' (length=58)
public 'line_items' =>
array (size=2)
0 =>
object(stdClass)[20]
...
1 =>
object(stdClass)[21]
...
public 'shipping_lines' =>
array (size=0)
empty
【问题讨论】: