Your First Call


Your first call against the API is special in two ways: It's the first call, and you should only execute it once. While you have an account number, you will also need a meter number in order to use the API. A FDXSubscriptionRequest call will give you that number.

The FedEx API provides only a basic XML interface; it uses neither SOAP nor REST. As such, requests are generated manually, then sent using cURL (pronounced see-URL), which provides HTTPS support. You then parse the response with SimpleXML. Support for cURL is provided through the libcurl library, which is not compiled by default. Appendix C has additional information on the configuration of the development box. The following function handles the basics of the request:

 function callFedEx($request) {   global $endpoint;   $ch = curl_init();   curl_setopt($ch, CURLOPT_URL, $endpoint);   curl_setopt($ch, CURLOPT_POST, TRUE);   curl_setopt($ch, CURLOPT_POSTFIELDS, $request);   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);   $response = curl_exec($ch);   if (curl_error($ch))   {     echo "<br>\n";     echo "Errors were encountered:";     echo curl_errno($ch);     echo curl_error($ch);     curl_close($ch);     return NULL;   }else   {     curl_close($ch);     $xml = simplexml_load_string($response);     return $xml;   } } 

After pulling the endpoint out of the Global scope, it sets up and executes the request. Should an error be encountered by cURL (this will likely only happen if your server can't contact FedEx), it will be returned. Otherwise, the response is parsed into a SimpleXML object and returned.

The request you need to execute should look something like this:

 $request = <<< XMLREQUEST <?xml version="1.0" encoding="UTF-8" ?> <FDXSubscriptionRequest xmlns:api="http://www.fedex.com/fsmapi"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="FDXSubscriptionRequest.xsd">   <RequestHeader>     <CustomerTransactionIdentifier>Test String</CustomerTransactionIdentifier>     <AccountNumber>1234567890</AccountNumber>   </RequestHeader>   <Contact>     <PersonName>Paul Reinheimer</PersonName>     <CompanyName>Wrox</CompanyName>     <Department>IT</Department>     <PhoneNumber>5191234567</PhoneNumber>     <E-MailAddress>paul@example.com</E-MailAddress>   </Contact>   <Address>     <Line1>123 Main Street</Line1>     <City>Example Ville</City>     <StateOrProvinceCode>NY</StateOrProvinceCode>     <PostalCode>10011</PostalCode>     <CountryCode>US</CountryCode>   </Address> </FDXSubscriptionRequest> XMLREQUEST; 

The format is pretty basic, and set by FedEx, but there are two elements I want to bring to your attention:

  • CustomerTransactionIdentifier — This is an element that can contain any string you want up to a maximum of 40 characters. The string will be returned to you in the response.

  • AccountNumber — This is the commercial account number that was added to the test server at your request.

Finally, here is the code to execute the request and obtain your meter number:

 $endpoint = ''; echo "<pre>"; echo htmlentities($request); echo "</pre>"; $response = callFedEx($request); echo "Your meter number is: {$response->MeterNumber}, write that down\n"; echo "<pre>"; print_r($response); echo "</pre>"; 

You should see the meter number output as shown in Figure 8-1.

image from book
Figure 8-1




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net