命名空间对象选择列表php

lmyy7pcs  于 2023-01-16  发布在  PHP
关注(0)|答案(1)|浏览(108)

我正在尝试使用联邦快递航运API的https://github.com/101medialab/shipping在我的网站。我不擅长与php,当我尝试的代码:
$calculator = new MediaLab\Shipping\Calculator\FedExCalculator($key,$password, $accountNumber, $meterNumber); $calculator->calculate($source, $destination, $shipment);
我可以看到它的React是这样的:

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
                    )

            )
    )

但是我对如何让这些值显示在选择列表中感到困惑。

ippsafx7

ippsafx71#

以下代码应构建一个选择框以显示结果:

$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 可能会有所帮助。

相关问题