phpfedex

Namespaced object to select list php


I am trying to use fedex shipping api by https://github.com/101medialab/shipping in my website. I am not good with php, when I try the code:

$calculator = new MediaLab\Shipping\Calculator\FedExCalculator($key,$password, $accountNumber, $meterNumber); $calculator->calculate($source, $destination, $shipment);

I can see a response from it like this:


    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
                    )

            )
    )

but I am confused on how can I get these value to show in a select list.


Solution

  • The following should build a select box to display the results:

    $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;
    

    Basically, you loop through the results and use the methods provided by the object to get the data you need. It might be helpful to see the class interface at https://github.com/101medialab/shipping/blob/master/src/MediaLab/Shipping/Model/EstimationInterface.php to all available methods.