123guy Posted January 22, 2013 Share Posted January 22, 2013 I am trying to integrate USPS Tracking info into my website. I have obtained a web tools user id from them...but I need to know how I send the request to the server and back to the site via php. Can any one help me out with this by providing some code, or by posting a good link to get me started? thanks Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/ Share on other sites More sharing options...
Jessica Posted January 22, 2013 Share Posted January 22, 2013 This forum is for help with code you wrote. Did you try googling the topic? They should have given you a URL for their API as well. Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/#findComment-1407588 Share on other sites More sharing options...
123guy Posted January 22, 2013 Author Share Posted January 22, 2013 yes I have, the issue is, I don't know how I call the server. I am getting very confused on how you use the url they gave you. I found this code here: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <?php //enter your username and password $data_string ='API=TrackV2&XML=<TrackRequest USERID="xxx"><TrackID ID="EJ958088694US"></TrackID></TrackRequest>'; // Get a CURL handle // Tell CURL the URL of the recipient script $curl_handle = curl_init (); curl_setopt ($curl_handle, CURLOPT_URL, 'http://testing.shippingapis.com/ShippingAPITest.dll'); // This section sets various options. See http://www.php.net/manual/en/function.curl-setopt.php curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl_handle, CURLOPT_POST, 1); curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string); // Perform the POST and get the data returned by the server. $result = curl_exec ($curl_handle) or die ("There has been a CURL_EXEC error"); // Close the CURL handle curl_close ($curl_handle); echo $result; ?> </head> <body> </body> </html> but on this everything displays on the same line. Maybe this one will work, but I need it to display correctly. it looks like this right now Your item was delivered at 1:39 pm on June 1 in WOBURN MA 01815.May 30 7:44 am NOTICE LEFT WOBURN MA 01815.May 30 7:36 am ARRIVAL AT UNIT NORTH READING MA 01889.May 29 6:00 pm ACCEPT OR PICKUP PORTSMOUTH NH 03801. Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/#findComment-1407592 Share on other sites More sharing options...
Jessica Posted January 22, 2013 Share Posted January 22, 2013 See, that's a whole hell of a lot different than your original post, isn't it. Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/#findComment-1407596 Share on other sites More sharing options...
kicken Posted January 22, 2013 Share Posted January 22, 2013 It probably gives you back an XML document with the information. Change your code to: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php //enter your username and password $data_string ='API=TrackV2&XML=<TrackRequest USERID="xxx"><TrackID ID="EJ958088694US"></TrackID></TrackRequest>'; // Get a CURL handle // Tell CURL the URL of the recipient script $curl_handle = curl_init (); curl_setopt ($curl_handle, CURLOPT_URL, 'http://testing.shippingapis.com/ShippingAPITest.dll'); // This section sets various options. See http://www.php.net/manual/en/function.curl-setopt.php curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl_handle, CURLOPT_POST, 1); curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string); // Perform the POST and get the data returned by the server. $result = curl_exec ($curl_handle) or die ("There has been a CURL_EXEC error"); // Close the CURL handle curl_close ($curl_handle); echo htmlentities($result); ?> </body> </html> That will let you see any XML tags the output may contain. If it is an XML document, you'll need to use something like SimpleXML or DOMDocument to parse it and extract the information you want, then output it in whatever format you want. Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/#findComment-1407606 Share on other sites More sharing options...
123guy Posted January 23, 2013 Author Share Posted January 23, 2013 it still displays all in one line with no breaks. <TrackResponse><TrackInfo ID="EJ958088694US"><TrackSummary>Your item was delivered at 1:39 pm on June 1 in WOBURN MA 01815.</TrackSummary><TrackDetail>May 30 7:44 am NOTICE LEFT WOBURN MA 01815.</TrackDetail><TrackDetail>May 30 7:36 am ARRIVAL AT UNIT NORTH READING MA 01889.</TrackDetail><TrackDetail>May 29 6:00 pm ACCEPT OR PICKUP PORTSMOUTH NH 03801.</TrackDetail></TrackInfo></TrackResponse> Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/#findComment-1407610 Share on other sites More sharing options...
Jessica Posted January 23, 2013 Share Posted January 23, 2013 If it is an XML document, you'll need to use something like SimpleXML or DOMDocument to parse it and extract the information you want, then output it in whatever format you want. ... Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/#findComment-1407611 Share on other sites More sharing options...
123guy Posted January 23, 2013 Author Share Posted January 23, 2013 sorry, didn't see that. I read it from email and it got cut short there. thanks Quote Link to comment https://forums.phpfreaks.com/topic/273510-usps-tracking-info/#findComment-1407637 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.