我正在尝试使用联邦快递api https://github.com/101medialab/shipping在我的网站。我不擅长php,当我尝试代码:
$calculator = new MediaLab'Shipping'Calculator'FedExCalculator($key,$password, $accountNumber, $meterNumber); $calculator->calculate($source, $destination, $shipment);
我可以看到它的响应如下:
Array
(
[0] => MediaLab'Shipping'Model'Estimation Object
(
[carrier:MediaLab'Shipping'Model'Estimation:private] => FedEx
[serviceName:MediaLab'Shipping'Model'Estimation:private] => First overnight
[serviceCode:MediaLab'Shipping'Model'Estimation:private] => FIRST_OVERNIGHT
[deliveryDate:MediaLab'Shipping'Model'Estimation:private] => DateTime Object
(
[date] => 2015-07-27 08:00:00
[timezone_type] => 3
[timezone] => Europe/Paris
)
[cost:MediaLab'Shipping'Model'Estimation:private] => MediaLab'Shipping'Model'Cost Object
(
[currency:MediaLab'Shipping'Model'Cost:private] => USD
[amount:MediaLab'Shipping'Model'Cost:private] => 161.59
)
)
[1] => MediaLab'Shipping'Model'Estimation Object
(
[carrier:MediaLab'Shipping'Model'Estimation:private] => FedEx
[serviceName:MediaLab'Shipping'Model'Estimation:private] => Priority overnight
[serviceCode:MediaLab'Shipping'Model'Estimation:private] => PRIORITY_OVERNIGHT
[deliveryDate:MediaLab'Shipping'Model'Estimation:private] => DateTime Object
(
[date] => 2015-07-27 10:30:00
[timezone_type] => 3
[timezone] => Europe/Paris
)
[cost:MediaLab'Shipping'Model'Estimation:private] => MediaLab'Shipping'Model'Cost Object
(
[currency:MediaLab'Shipping'Model'Cost:private] => USD
[amount:MediaLab'Shipping'Model'Cost:private] => 71.65
)
)
)
但是我很困惑,我如何才能得到这些值显示在一个选择列表
下面应该构建一个选择框来显示结果:
$calculator = new MediaLab'Shipping'Calculator'FedExCalculator($key,$password, $accountNumber, $meterNumber);
$results = $calculator->calculate($source, $destination, $shipment);
var $html = '<select>';
foreach ($results as $result) {
$cost = $result->getCost()->getCurrency() . $result->getCost()->getAmount();
$html .= '<option name="' . $result->getServiceCode() . '">' . $result->getCarrier() . ' - ' . $result->getServiceName() . ' - ' . $result->getDeliveryDate() . ' - ' . $cost . '</option>';
}
$html = '</select>';
echo $html;
基本上,循环遍历结果并使用对象提供的方法来获取所需的数据。在https://github.com/101medialab/shipping/blob/master/src/MediaLab/Shipping/Model/EstimationInterface.php查看所有可用方法的类接口可能会有所帮助。