What's new

PHP Parcel tracking

Crypto Pitt

Forum Guru
Elite
Joined
Nov 19, 2020
Posts
2,683
Reaction
4,446
Points
1,546
How to be able to display where is the parcel's exact location using php.
 
To display the exact location of a parcel using PHP, you will need to use an API provided by the shipping company or a third-party service that provides tracking information. Here are the basic steps you can follow:

1. Choose an API: Look for a shipping company (such as UPS or FedEx) or a third-party service (such as AfterShip or TrackingMore) that provides tracking APIs. Check the documentation and pricing of the API to make sure it meets your requirements.

2. Obtain API key: Register for the API and obtain an API key or authentication token that can be used to make requests to the API.

3. Make API requests: Use PHP cURL or a library like Guzzle to make HTTP requests to the API endpoint with the tracking number of the parcel as a parameter. The response will contain the tracking information, including the location of the parcel.

4. Parse and display the data: Parse the JSON or XML response and extract the relevant data (such as the current location) using PHP. You can then display the data on your website or application.

Here is a basic example of how to make an API request using cURL in PHP:

PHP:
$tracking_number = '1234567890'; // replace with your tracking number
$api_key = 'your_api_key'; // replace with your API key

$url = 'https://api.shippingcompany.com/tracking?tracking_number='.$tracking_number.'&api_key='.$api_key;

// create cURL request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// parse JSON response
$data = json_decode($response, true);

// get current location
$location = $data['tracking_info'][0]['location'];

// display location
echo 'Current location: '.$location;

Note that the exact implementation will depend on the specific API you are using and the data format of the response.
 
Back
Top