FedEx Shipping API using PHP

4.6k views Asked by At

I am trying to Integrating fedex api using php but there are some errors .i am using following code:

  <?php

   $url = 'https://sandbox-api.postmen.com/v3/rates';
   $method = 'POST';
   $headers = array(
       "content-type: application/json",
       "postmen-api-key: 6ea35e71-385c-8cda-5729-f7d02c1ceb5a"
   );
   $body = '{"async":false,"shipper_accounts":[{"id":"00000000-0000-0000-0000-000000000000"}],"is_document":false,"shipment":{"ship_from":{"contact_name":"Elmira Zulauf","company_name":"Kemmer-Gerhold","street1":"662 Flatley Manors","country":"HKG","type":"business"},"ship_to":{"contact_name":"Dr. Moises Corwin","phone":"1-140-225-6410","email":"[email protected]","street1":"28292 Daugherty Orchard","city":"Beverly Hills","postal_code":"90209","state":"CA","country":"USA","type":"residential"},"parcels":[{"description":"Food XS","box_type":"custom","weight":{"value":2,"unit":"kg"},"dimension":{"width":20,"height":40,"depth":40,"unit":"cm"},"items":[{"description":"Food Bar","origin_country":"USA","quantity":2,"price":{"amount":3,"currency":"USD"},"weight":{"value":0.6,"unit":"kg"},"sku":"imac2014"}]}]}}';

   $curl = curl_init();

   curl_setopt_array($curl, array(
       CURLOPT_RETURNTRANSFER => true,
       CURLOPT_URL => $url,
       CURLOPT_CUSTOMREQUEST => $method,
       CURLOPT_HTTPHEADER => $headers,
     CURLOPT_POSTFIELDS => $body
   ));

   $response = curl_exec($curl);
   $err = curl_error($curl);

   curl_close($curl);

   if ($err) {
     echo "cURL Error #:" . $err;
   } else {
     echo $response;
   }

This is the result I get from the code :

  {"meta":{"code":4105,"message":"Invalid URL or API key.","details":[],"retryable":false},"data":{}}

And i have using some other code is :

    <?php
            //your account details here
            $key = '';
            $password = ' ';
            $account_number = '';
            $meter_number = '';

            $xml = '<?xml version="1.0" encoding="UTF-8"?>
            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://fedex.com/ws/rate/v13"><SOAP-ENV:Body><ns1:RateRequest>
            <ns1:WebAuthenticationDetail>
            <ns1:UserCredential>
            <ns1:Key>'.$key.'</ns1:Key>
            <ns1:Password>'.$password.'</ns1:Password>
            </ns1:UserCredential></ns1:WebAuthenticationDetail> 
            <ns1:ClientDetail>
            <ns1:AccountNumber>'.$account_number.'</ns1:AccountNumber>
            <ns1:MeterNumber>'.$meter_number.'</ns1:MeterNumber>
            </ns1:ClientDetail>
            <ns1:TransactionDetail><ns1:CustomerTransactionId> *** Rate Request v13 using PHP ***</ns1:CustomerTransactionId></ns1:TransactionDetail><ns1:Version><ns1:ServiceId>crs</ns1:ServiceId><ns1:Major>13</ns1:Major><ns1:Intermediate>0</ns1:Intermediate><ns1:Minor>0</ns1:Minor></ns1:Version><ns1:ReturnTransitAndCommit>true</ns1:ReturnTransitAndCommit><ns1:RequestedShipment>
            <ns1:DropoffType>REGULAR_PICKUP</ns1:DropoffType>
            <ns1:ServiceType>FEDEX_EXPRESS_SAVER</ns1:ServiceType>
            <ns1:PackagingType>YOUR_PACKAGING</ns1:PackagingType>
            <ns1:TotalInsuredValue><ns1:Currency>USD</ns1:Currency></ns1:TotalInsuredValue>
            <ns1:Shipper><ns1:Contact><ns1:PersonName>Sender Name</ns1:PersonName><ns1:CompanyName>Sender Company Name</ns1:CompanyName><ns1:PhoneNumber></ns1:PhoneNumber></ns1:Contact><ns1:Address><ns1:StreetLines></ns1:StreetLines><ns1:City></ns1:City><ns1:StateOrProvinceCode></ns1:StateOrProvinceCode>
            <ns1:PostalCode>94040</ns1:PostalCode><ns1:CountryCode>US</ns1:CountryCode></ns1:Address></ns1:Shipper>
            <ns1:Recipient><ns1:Contact><ns1:PersonName>Recipient Name</ns1:PersonName><ns1:CompanyName>Company Name</ns1:CompanyName><ns1:PhoneNumber></ns1:PhoneNumber></ns1:Contact><ns1:Address><ns1:StreetLines></ns1:StreetLines><ns1:City></ns1:City><ns1:StateOrProvinceCode></ns1:StateOrProvinceCode>
            <ns1:PostalCode>98052</ns1:PostalCode>
            <ns1:CountryCode>US</ns1:CountryCode><ns1:Residential>false</ns1:Residential></ns1:Address></ns1:Recipient><ns1:ShippingChargesPayment><ns1:PaymentType>SENDER</ns1:PaymentType><ns1:Payor>
            <ns1:ResponsibleParty>
            <ns1:AccountNumber>'.$account_number.'</ns1:AccountNumber>
            </ns1:ResponsibleParty>
            </ns1:Payor></ns1:ShippingChargesPayment>
            <ns1:RateRequestTypes>ACCOUNT</ns1:RateRequestTypes><ns1:PackageCount>1</ns1:PackageCount><ns1:RequestedPackageLineItems><ns1:SequenceNumber>1</ns1:SequenceNumber>
            <ns1:GroupPackageCount>1</ns1:GroupPackageCount>
            <ns1:Weight><ns1:Units>LB</ns1:Units><ns1:Value>20</ns1:Value></ns1:Weight>
            <ns1:Dimensions>
            <ns1:Length>10</ns1:Length>
            <ns1:Width>10</ns1:Width>
            <ns1:Height>10</ns1:Height>
            <ns1:Units>IN</ns1:Units>
            </ns1:Dimensions>
            </ns1:RequestedPackageLineItems>
            </ns1:RequestedShipment></ns1:RateRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>';

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://wsbeta.fedex.com:443/web-services');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            $result_xml = curl_exec($ch);

            // remove colons and dashes to simplify the xml
            $result_xml = str_replace(array(':','-'), '', $result_xml);
             $result = @simplexml_load_string($result_xml);
            //$array_data = json_decode(json_encode(simplexml_load_string($result_xml)), true);
            // print_r($result); die();


            print '<pre>';
            print 'Rate: $';
            //echo  $result->Amount;
            //echo $array_data->SOAPENVBody;
            print '<hr/>';
            echo $array_data->Amount;
            print_r($result);

That will show the following error

                       [v13Severity] => ERROR
                        [v13Source] => prof
                        [v13Code] => 1000
                        [v13Message] => Authentication Failed

Anyone can help me please.I am trying to find solution from last 2 days. can one have any idea about this or have any api regarding fedex shiping rate. I have to to find only shipping rate with this api.

Thanks in advance.

1

There are 1 answers

0
Janatbek On

your url is pointing to rate api, find right url for shipping.