Jump to content
Stef's Coding Community

Api's (Communicating With Other Websites)


Recommended Posts

I was just wondering if I could get a short & sweet explanation for how to exchange data with other websites.

 

A common example would be requesting the shipping-cost for a package from UPS.

 

I'm pretty fuzzy with how one "catches" the data replied to you (from UPS), and how you would get that data put into a PHP variable. Once this "answer" to my request is in a PHP variable, I would be able to figure it out from there.

 

Their example code for a request:

 

<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
	<AccessLicenseNumber>Your_License</AccessLicenseNumber>
<UserId>Your_ID</UserId>
<Password>Your_Password</Password>
</AccessRequest>
<?xml version="1.0"?>
<RatingServiceSelectionRequest xml:lang="en-US">
 <Request>
   <TransactionReference>
     <CustomerContext>Rating and Service</CustomerContext>
     <XpciVersion>1.0</XpciVersion>
   </TransactionReference>
<RequestAction>Rate</RequestAction>
<RequestOption>Rate</RequestOption>
 </Request>
   <PickupType>
 	<code>07</code>
 	<Description>Rate</Description>
   </PickupType>
 <Shipment>
   	<Description>Rate Description</Description>
   <Shipper>
     <Name>Name</Name>
     <PhoneNumber>1234567890</PhoneNumber>
     <ShipperNumber>Ship Number</ShipperNumber>
     <address>
       <AddressLine1>Address Line</AddressLine1>
       <City>City</City>
       <StateProvinceCode>NJ</StateProvinceCode>
       <PostalCode>07430</PostalCode>
       <CountryCode>US</CountryCode>
     </address>
   </Shipper>
   <ShipTo>
     <CompanyName>Company Name</CompanyName>
     <PhoneNumber>1234567890</PhoneNumber>
     <address>
       <AddressLine1>Address Line</AddressLine1>
       <City>Corado</City>
       <PostalCode>00646</PostalCode> 
       <CountryCode>PR</CountryCode>
     </address>
   </ShipTo>
   <ShipFrom>
     <CompanyName>Company Name</CompanyName>
     <AttentionName>Attention Name</AttentionName>
     <PhoneNumber>1234567890</PhoneNumber>
     <FaxNumber>1234567890</FaxNumber>
     <address>
       <AddressLine1>Address Line</AddressLine1>
	<City>Boca Raton</City>
       <StateProvinceCode>FL</StateProvinceCode>
       <PostalCode>33434</PostalCode> 
       <CountryCode>US</CountryCode>
     </address>
   </ShipFrom>
 	<Service>
   		<code>03</code>
 	</Service>
 	<PaymentInformation>
      	<Prepaid>
       		<BillShipper>
         			<AccountNumber>Ship Number</AccountNumber>
       		</BillShipper>
     		</Prepaid>
 	</PaymentInformation>
 	<Package>
     		<PackagingType>
        	<code>02</code>
       		<Description>Customer Supplied</Description>
     		</PackagingType>
     		<Description>Rate</Description>
     		<PackageWeight>
     			<UnitOfMeasurement>
     			  <code>LBS</code>
     			</UnitOfMeasurement>
        	<Weight>10</Weight>
     		</PackageWeight>   
  	</Package>
   <ShipmentServiceOptions>
     <OnCallAir>
	<Schedule> 
		<PickupDay>02</PickupDay>
		<Method>02</Method>
	</Schedule>
     </OnCallAir>
   </ShipmentServiceOptions>
 </Shipment>
</RatingServiceSelectionRequest>

 

Their example of a Response (I have no idea how I would even get or work with this)

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<RatingServiceSelectionResponse>
   <Response>
       <TransactionReference>
           <CustomerContext>Rating and Service</CustomerContext>
           <XpciVersion>1.0</XpciVersion>
       </TransactionReference>
       <ResponseStatusCode>1</ResponseStatusCode>
       <ResponseStatusDescription>Success</ResponseStatusDescription>
   </Response>
   <RatedShipment>
       <Service>
           <Code>03</Code>
       </Service>
       <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning>
       <BillingWeight>
           <UnitOfMeasurement>
               <Code>LBS</Code>
           </UnitOfMeasurement>
           <Weight>10.0</Weight>
       </BillingWeight>
       <TransportationCharges>
           <CurrencyCode>USD</CurrencyCode>
           <MonetaryValue>52.16</MonetaryValue>
       </TransportationCharges>
       <ServiceOptionsCharges>
           <CurrencyCode>USD</CurrencyCode>
           <MonetaryValue>0.00</MonetaryValue>
       </ServiceOptionsCharges>
       <TotalCharges>
           <CurrencyCode>USD</CurrencyCode>
           <MonetaryValue>52.16</MonetaryValue>
       </TotalCharges>
       <GuaranteedDaysToDelivery/>
       <ScheduledDeliveryTime/>
       <RatedPackage>
           <TransportationCharges>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>52.16</MonetaryValue>
           </TransportationCharges>
           <ServiceOptionsCharges>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>0.00</MonetaryValue>
           </ServiceOptionsCharges>
           <TotalCharges>
               <CurrencyCode>USD</CurrencyCode>
               <MonetaryValue>52.16</MonetaryValue>
           </TotalCharges>
           <Weight>10.0</Weight>
           <BillingWeight>
               <UnitOfMeasurement>
                   <Code>LBS</Code>
               </UnitOfMeasurement>
               <Weight>10.0</Weight>
           </BillingWeight>
       </RatedPackage>
   </RatedShipment>
</RatingServiceSelectionResponse>

 

Any feedback would be appreciated :)

Thank you!

Link to post
Share on other sites

The short answer is this -- an API is a way for an external person/machine to access a database and retrieve information from it. Usually, you'd ask for information using a specific URL, and you'd get the data you need in some sort of structured format -- either JSON or XML usually (this particular response returns XML). You'd then use PHP to parse that result and use the data however you needed. I'd suggest doing a search for tutorials on building a basic API to understand how it works, and specifically how to process the XML returned from an API request.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...