【发布时间】:2018-07-19 02:55:34
【问题描述】:
我在 PHP 中有这段代码(它只是从 API 检索一些交付方法,API 是第 3 方)
use MPAPI\Services\DeliveryMethods;
use MPAPI\Services\Deliveries;
use MPAPI\Entity\PartnerDelivery;
use MPAPI\Entity\GeneralDelivery;
$deliveryMethods = new DeliveryMethods($mpapiClient);
$response = $deliveryMethods->get();
var_dump($response);
响应是:
array(2) {
[0]=>
object(MPAPI\Entity\DeliveryMethod)#35 (1) {
["data":protected]=>
array(7) {
["id"]=>
string(4) "Test"
["title"]=>
string(18) "Testovacia doprava"
["price"]=>
int(10)
["cod_price"]=>
int(10)
["free_limit"]=>
int(0)
["delivery_delay"]=>
int(5)
["is_pickup_point"]=>
bool(false)
}
}
[1]=>
object(MPAPI\Entity\DeliveryMethod)#36 (1) {
["data":protected]=>
array(7) {
["id"]=>
string(3) "UPS"
["title"]=>
string(22) "Kuriérska služba UPS"
["price"]=>
int(5)
["cod_price"]=>
int(0)
["free_limit"]=>
int(0)
["delivery_delay"]=>
int(4)
["is_pickup_point"]=>
bool(false)
}
}
}
我想在 PHP 中访问它,所以我的 foreach 循环看起来像:
<?php foreach ($response[0]->data as $item) { ?>
// ...
<?php } ?>
但我得到一个错误:
致命错误:未捕获的错误:无法访问受保护的属性 MPAPI\Entity\DeliveryMethod::$data in /data/web/virtuals/175241/virtual/www/doprava.php:39 堆栈跟踪:#0 {main} 在 /data/web/virtuals/175241/virtual/www/doprava.php 上抛出 第 39 行
那么如何在 PHP 中正确读取这些数据呢?
如果我像这样更改 foreach 循环:
<?php foreach ($response as $item) { ?>
// ...
<?php } ?>
我会得到另一个错误:
致命错误:未捕获错误:无法使用对象类型 MPAPI\Entity\DeliveryMethod 作为数组
在 API 的文档中没有关于 https://github.com/mallgroup/mpapi-client-php/blob/master/doc/DELIVERIES.md
【问题讨论】:
-
你看过API的文档吗,这些私有属性可能有Getter。
-
我查看了API的文档,但是没有github.com/mallgroup/mpapi-client-php/blob/master/doc/…