In this post, we will explain to you how we use FedEx api to create a shipping label in PHP. First, you have to send the shipment request to confirm shipping then print the shipping label.
First use your FedEx Test developer access to generate your first label. You can not use production account access directly for label generation, this will gives you an authentication error.
To get production account access, first you have to generate some FeDex labels like Ground Label, Label with Signature, COD etc. Then have to send these labels to Fedex support for evaluation purpose. They will review your labels and verify it.
Then they will actiavte your Live Fedex Shipping API access, just replace with test account access and use it for live transactions on your application.
Generate FedEx shipping label via API in PHP:
Step 1: We are using a PHP soap client request to generate FedEx label.
You can download official Fedex Soap Request library from fedex developer account.
Firstly include “ShipService_v19.wsdl” file in your PHP file. Make sure that API Eeb Request URL point to test fedex api server in WSDL file. (For Production account, change it with live API web request URL).
Step 2: Create a Label.php file using the below code. Which call the FedEx test API request url.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
<?php $path_to_wsdl = "./ShipService_v19.wsdl"; ini_set("soap.wsdl_cache_enabled", "0"); $client = new SoapClient($path_to_wsdl, array('trace' => 1)); $request['WebAuthenticationDetail'] = array( 'ParentCredential' => array( 'Key' => 'USE Test KEY HERE', 'Password' => 'USE Test Password Here' ), 'UserCredential' => array( 'Key' => 'USE Test KEY HERE', 'Password' => 'USE Test Password Here' ) ); $request['ClientDetail'] = array( 'AccountNumber' => 'Use Account Number Here', 'MeterNumber' => 'Use Meter Number Here' ); $request['TransactionDetail'] = array('CustomerTransactionId' => 'ProcessShip_Basic'); $request['Version'] = array( 'ServiceId' => 'ship', 'Major' => '19', 'Intermediate' => '0', 'Minor' => '0' ); $request['RequestedShipment'] = array( 'ShipTimestamp' => '2016-04-26T15:46:24-06:00', 'DropoffType' => 'REGULAR_PICKUP', // valid values REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER and STATION 'ServiceType' => 'PRIORITY_OVERNIGHT', // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, ... 'PackagingType' => 'YOUR_PACKAGING', // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ... 'TotalWeight' => array( 'Value' => 50.0, 'Units' => 'LB' // valid values LB and KG ), 'Shipper' => array( 'Contact' => array( 'PersonName' => 'Person Name', 'CompanyName' => 'Company Name', 'PhoneNumber' => '400-2345-3489' ), 'Address' => array( 'StreetLines' => array('Sender Address'), 'City' => 'City Name', 'StateOrProvinceCode' => 'State Code', // for Example TX 'PostalCode' => 'Postal Code', 'CountryCode' => 'Country Code' // For example US ) ), 'Recipient' => array( 'Contact' => array( 'PersonName' => 'Liana', 'CompanyName' => 'Erida', 'PhoneNumber' => '1234567890' ), 'Address' => array( 'StreetLines' => array('Recipient Address'), 'City' => 'City Name', 'StateOrProvinceCode' => 'State Code', // For Example HR 'PostalCode' => 'Postal Code', 'CountryCode' => 'Country Name', 'Residential' => true ) ), 'LabelSpecification' => array( 'LabelFormatType' => 'COMMON2D', // valid values COMMON2D, LABEL_DATA_ONLY 'ImageType' => 'PDF', // valid values DPL, EPL2, PDF, ZPLII and PNG 'LabelStockType' => 'PAPER_7X4.75' ), 'PackageCount' => 1, 'PackageDetail' => 'INDIVIDUAL_PACKAGES', 'RequestedPackageLineItems' => array( '0' => array( 'SequenceNumber'=>1, 'GroupPackageCount'=>1, 'Weight' => array( 'Value' => 50.0, 'Units' => 'LB' ), 'Dimensions' => array( 'Length' => 108, 'Width' => 5, 'Height' => 5, 'Units' => 'IN' ), 'CustomerReferences' => array( '0' => array( 'CustomerReferenceType' => 'CUSTOMER_REFERENCE', // valid values CUSTOMER_REFERENCE, INVOICE_NUMBER, P_O_NUMBER and SHIPMENT_INTEGRITY 'Value' => 'GR4567892' ), '1' => array( 'CustomerReferenceType' => 'INVOICE_NUMBER', 'Value' => 'INV4567892' ), '2' => array( 'CustomerReferenceType' => 'P_O_NUMBER', 'Value' => 'PO4567892' ) ), 'SpecialServicesRequested' => array( 'SpecialServiceTypes' => array('COD'), 'CodDetail' => array( 'CodCollectionAmount' => array( 'Currency' => 'USD', 'Amount' => 150 ), 'CollectionType' => 'ANY' // ANY, GUARANTEED_FUNDS ) ) ) ) ); $response = $client->processShipment($request); echo "<pre>"; print_r($response); ?> |
After this request, success response will shows on screen, along with the Fedex Label Image code. You can create label image using that response code.
You can also create a custom fedex shipping label as per your requirement. Follow this Fedex Developer Guide Reference.
Also Read This:
Create Fedex Multiple Package Shipment Label
How to Install Magento Security Patch via Putty
We can also use simple xml request parameter for creating FedEx api label in php using curl method. Although above method is quite easy to implement on website.
Appreciate the code, but generated pdf files were of zero kb, any idea?
Hello Kausik,
Which service type you use for generate shipping label ? Can you share your soap request code ?
generated pdf files 0 kb, can you help, please
Hi Ahmad,
I think you should check the error response first. If you getting label image data in response than it should be problem in convert it into an image.
Hi Harish,
I simply uploaded ShipService_v17.wsdl and label.php with your code under a folder and tried to execute the php file. Do I need to do any thing else? If you share your email id, I can share test key and other details.
Have you change shiptimestamp with next shipping date in request ? Still facing issue, mail me info on this id “[email protected]”
Hi again,
in your code what are parent and user credential? IN FEDX php code also I don’t see any such reference. Can you please explain?
$request[‘WebAuthenticationDetail’] = array(
‘ParentCredential’ => array(
‘Key’ => ‘EPLkBjBoWJL4gWDz’,
‘Password’ => ‘6RrOFaG3V9hP2tRwll58Tunb0’
),
‘UserCredential’ => array(
‘Key’ => ‘EPLkBjBoWJL4gWDz’,
‘Password’ => ‘6RrOFaG3V9hP2tRwll58Tunb0’
)
Hi, both takes same credential.
Dear Harish,
I mailed you the file with code in the morning, did you get chance to check it?
Regards,
Kausik
Hi Kausik,
I tried your code with my fedex test account access. Its work fine. You should change Shipper Address and Recipient address by own developer test address.
When you create fedex development key, they will mail you test address for using fedex services.
Hi Harish,
I just mailed you, kindly check.
Hello, i just email you working code. Just edit shipper and recipient address and its work fine.
sir i need some help to integrate fedex api to my website
,i want to check availiability of service of fedex depend on postal code
Hello sandeep,
I think for this you need to run Fedex Postal Code Inquiry api. Which give result shipping available on particular post code or not.
I used same Test key & password in credentials
but I am getting this error
Severity: ERROR
Source: prof
Code: 1000
Message: Authentication Failed
Hi Tut,
You must use Test Development Access, which you got at the time of registration in fedex account.
Also for label request change shipper and recipient address as per your fedex account.
That worked. Thanks for you help.
Hello Harish ,
I did all chages suggested by you still it shows authentication failed error.Please check my attached file on your mail and suggest me appropriate changes.
Which account number is used in script?
That is define in Test password mail.Or that are defined in shipper and recipient address.
Hi Rohit,
Authentication error comes when you trying to create fedex label using production account access without evaluation process. Are you using right developer access in it ?
Account number you received with Password email from Fedex.
Is it require to enable test password of web services on fedex?
No, its enable by default.
Still i facing same error. please check my script on your mail.
Ok i will check, can you also send me your test access to check ?
Please keep thrwiong these posts up they help tons.
What is difference between ParentCredential and USER credentials?
Both will take same credential.
Hi Harish,
Hello Harish,
I just mailed you the new file with script , did you get chance to check it?
Hi Harish,
I am doing integration of fedex api in php, I use rate available api to get list of services. But can you help to for next step. i think we have to place shipping order to get tracking code.
So, it will be helpful if you provide some information on it.
Hi sam,
You have to write Fedex Shipping label code to get tracking code. Just place a test order with fedex service type to create shipping label. I hope this post will help to do this.
Thanks
Thanks for your prompt reply, as in create shipment there are different folders and each have shipment file init. so which one i use as i want to create shipment for india. (like: form india to india).
Yes exactly , i also wants same.. when using india to india address,, fedexp apj not supported at all.
Hello ,
Which Account number used in API credential?
Hi,
Use account number your got in Developer Test Key Registration email from Fedex.
Hello Harish,
Please check my email.
Hello Harish ,
Anything wrong in the script.That sent via mail.
Hello Harish,
Plz give me some suggestions for NON COD basis labels.
Hi Rohit,
If you are using FEDEX_GROUND service type than COD parameter is required in label request. For print Non COD Fedex label for other service, just remove these request parameter from label request code.
‘SpecialServicesRequested’ => array(
‘SpecialServiceTypes’ => ‘COD’,
‘CodDetail’ => array(
‘CodCollectionAmount’ => array(
‘Currency’ => ‘USD’,
‘Amount’ => ‘10.0’
),
‘CollectionType’ => ‘ANY’
)
)
I have used this code , but when i enter indian address . i have received following error;
[Notifications] => stdClass Object
(
[Severity] => ERROR
[Source] => ship
[Code] => 2449
[Message] => Customs Clearance Detail is required
[LocalizedMessage] => Customs Clearance Detail is required
)
but when i use outside india .. its working ..
so my concern is that , why i receiving that customclearance error in india address.
Hi Piyush,
Fedex have different policies for each country. If custom clearance is required for India country than you should pass those parameters in API request. This given code belong to US country.
Thanks
Hello , Could you please send me code that is related to india.. ?
Hello , Could you please send me code that is related to india.. ? please send Label.php according to india shipping label format.
Well, currently i don’t have Fedex shipping code for India country basis. You should check Fedex api documentation to create an request for it.
Hi Harish,
Where can i get the source ‘ShipService_v19.wsdl’ ? Please
let me know.
Thanks for your help
Hi Ramesh,
You can download all shipping source files from Fedex account. Their are all Fedex web service source and documentation available.
For now you can download wsdl file from here : https://codefixup.com/fedex/ShipService_v19.wsdl
Hi,
This code really help me to fix shipping api call. Thanks and very much appreciated.
I have used your code but I am unable to generate label. So can you please let me know how can I generate the fedex label in png format?
Hi,
Have you check label generation API call response. Should cross check it, it must parameter issue.
For png label, change parameter value in LabelSpecification parameter. Follow Fedex official developer documentation for valid parameters and corresponding values.
Thanks
Hello Harish,
have you script for Fedex Tracking API in php.
Please let me know.
Yes i can create script for that.
Thanks
Can you please send me the full working code on my email.I am new in Apis integration .
Thanks In Advance.
🙂
Hi Karan,
I have explained here how to execute FedEX api call and print shipping label. You should follow fedex official documentation to create API request as per your need. If you need my service to create FedEX APIs call, inbox me on [email protected].
Thanks
Function (“processShipment”) is not a valid method for this service
Hi Mustafa,
This is a valid function, above mentioned Fedex API code is valid. I have tested so many times.
What issue you facing ? Please tell me.
Thanks
Hi,
I sadly get empty PDF Files (0 kb). Whats wrong? 🙁
Best Regards
Philipp
Hi Philipp,
I think you should check the response first. Something not right in your API request. In API response you will get the error detail.
Just print_r($response); this.
Thanks
Thanks for the hint! 🙂 There was something wrong with the Zip Code. Now it works.
Best Regards
Philipp
OK great…
Hello,
I am new for fedex system. I want to implement fedex API for generate shipping label. Can you provide me working code for testing purpose.
Please reply me as soon as possible.
Thanks
Ansari Umair
Hi Umair,
I have mentioned working code here. You can implement using it. What problem you facing to use it ?
If you need my service to create fedex shipping label code, please inbox me : [email protected]
Thanks
Hello,
I am running into an issue with creating a Return Label. I am getting PHP errors rather than it giving me XML output error. Uncaught SoapFault exception: [SOAP-ENV:Server] SoapClient->__call(‘processShipment’, Array)
All I changed was this:
‘SpecialServicesRequested’ => array(
‘SpecialServiceTypes’ => ‘RETURN_SHIPMENT’,
‘ReturnShipmentDetail’ => array(
‘ReturnType’ => ‘FEDEX_TAG’,
‘Rma’ => array(
‘Number’ => ‘012’,
‘Reason’ => ‘reason’
)
)
)
I also tried this:
‘SpecialServicesRequested’ => array(
‘SpecialServiceTypes’ => array(‘RETURN_SHIPMENT’),
‘ReturnShipmentDetail’ => ‘ReturnType’,
‘Value’ => ‘PRINT_RETURN_LABEL’
)
Hi Mr. Harish can you guide me how to pass multiple package shipment??
Hi Dhananjay,
I already written an post regarding create fedex multiple package shipment. Have you check this : https://www.codefixup.com/how-to-create-fedex-multiple-package-shipment-label-using-api-call-in-php/ ?
Hi,
I have liked the page on facebook for unblock the Label.php content, But Still I am not able to see the content code of Label.php, So can you please provide the code for Label.php.
Thanks.
I need to print fedex shiping labels on click. Can you send me code files please?
Hi Vince,
I already mentioned code details above, you can make your own script using it. If you facing issues, i will make Fedex API script for you, but this is paid service.
Thanks
Hi Harish,
thanks for your great script. we have extended the script that we can generate commercial invoices for international shipments. we would like to integrate our own company logo like it is when we generate the invoice by the fedex webpage.
do you have any sample code or a tip?
Thanks.
Hi,
I don’t have ready code for this, but I think you can do this in two ways, First from the FedEx account setting and second you have to find the relevant parameter in shipment create API documentation for this.
Hi Harish
I tried your code but getting some issues regarding credentials showing when i tried to call file on the frontend.
I had sent you a mail regarding this issue yesterday.pls review that and help me
ERROR: SOAP Fault: (faultcode: Client, faultstring: SOAP-ERROR: Encoding: object has no ‘UserCredential’ property) in /var/www/html/Label.php on line 140
Thanks
Hi Vivek,
I think you missed User Credential parameters in Soap API request, please check official documentation.
Hi Harish,
I tried your code and a response is coming but the label image is not generated.
ex:
[Label] => stdClass Object
(
[Type] => OUTBOUND_LABEL
[ShippingDocumentDisposition] => RETURNED
[ImageType] => PDF
[Resolution] => 200
[CopiesToPrint] => 1
[Parts] => stdClass Object
(
[DocumentPartSequenceNumber] => 1
[Image] => %PDF-1.4
Thanks
Hi Komal, i think this is becuase of incorrect parameters request. Please cross check it again Or i suggest to buy working Fedex Shipping Label code from my website.
Hi Harish,
I tried your code and a response is coming and the image also generating but getting a warning
“Unable to obtain courtesy rates”.
pls, help me on this why it is showing warning earlier it does not show any warning.
stdClass Object
(
[HighestSeverity] => ERROR
[Notifications] => stdClass Object
(
[Severity] => ERROR
[Source] => ship
[Code] => 2467
[Message] => shippingChargesPayment is required
[LocalizedMessage] => shippingChargesPayment is required
)
[TransactionDetail] => stdClass Object
(
[CustomerTransactionId] => ProcessShip_Basic
)
[Version] => stdClass Object
(
[ServiceId] => ship
[Major] => 19
[Intermediate] => 0
[Minor] => 0
)
)
Hello please tell me what should i do
what i miss
Hi, you have missed ‘ShippingChargesPayment’ parameter in create shipment API request. Use below parameters:
‘ShippingChargesPayment’ => array(
‘PaymentType’ => ‘SENDER’,
‘Payor’ => array(
‘ResponsibleParty’ => array(
‘AccountNumber’ => ‘********’, // enter your Fedex Account Number
‘Contact’ => null,
‘Address’ => array(
‘CountryCode’ => ‘IN’
)
)
)
)