PHP Script - How to track courier service using tracking number from my website

I need like,

http://www.trackcourier.in/

The customer chooses selected courier service and enters tracking id then clicks its redirect to the appropriate courier service website.

eg: If tracking id 12345 the redirect link should be https://www.fedex.com/apps/fedextrack/index.html?tracknumbers=12345&cntry_code=in

How to do for India courier services?

You can redirect with the header() function.

Thank you for reply, i am new in PHP, could you pls help me to write?

What code have you got so far?

You would need to find out from each of those courier services how to access their public API for tracking. Sometimes you can see from their public tracking site how it’s done, but you have to be prepared for them to change that without warning - if you’re using an API, that happens less often, and you get warning.

@chorn

<!DOCTYPE HTML>
<html>  
<body>

<form action="#" method="post">
Select Courier :
<select name="courier">
  <option value="">--Please choose an option--</option>
  <option value="professional_courier">Professional Courier</option>
  <option value="india_post">India Post</option>
</select>

Trackingid: <input type="text" name="trackingid"><br>
<input type="submit">

</form>



<?php 
if (!empty($_POST)): header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0");
endif;
?>

</body>
</html>

How to track more than one service, now my code track only one service, how can i add india post if customer choose india post courier service url: https://www.indiapost.gov.in/_layouts/15/dop.portal.tracking/trackconsignment.aspx

if (!empty($_POST['courier']) && !empty($_POST['trackinid']) {
    switch ($_POST['courier']) {
        case 'professional_courier':
            $url = 'https://www.tpcindia.com/Tracking2014.aspx?id='.$_POST["trackingid"].'&type=0&service=0';
            break;
        case 'india_post':
            $url = 'https://www.indiapost.gov.in/_layouts/15/dop.portal.tracking/trackconsignment.aspx';
            break;
        default:
            throw new RuntimeException(sprintf('Unknown courier "%s"', $_POST['courier']);
    }
    header('Location: '.$url);
}

As that page has a Captcha test on it to prevent such things, I would imagine the only way to automate is to get their API, if they have one.

@rpkamp

Can i get help based on your code,

<!DOCTYPE HTML>
<html>
<body>
    <form action="#" method="post">
        Select Courier :
        <select name="courier">
            <option value="">--Please choose an option--</option>
            <option value="professional_courier">Professional Courier</option>
            <option value="india_post">India Post</option>
        </select>

        Trackingid: <input type="text" name="trackingid"><br>
        <input type="submit">

    </form>
    <?php
    // if (!empty($_POST)): header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0");
    // endif;

    if (!empty($_POST['courier']) && !empty($_POST['trackingid'])) {
        switch ($_POST['courier']) {
            case 'professional_courier':
                $url = 'https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0';
                break;
            case 'india_post':
                $url = 'https://www.indiapost.gov.in/_layouts/15/dop.portal.tracking/trackconsignment.aspx';
                break;
            default:
                throw new RuntimeException(sprintf('Unknown courier "%s"', $_POST['courier']));

                header('Location: ' . $url);
        };
    };
    ?>

</body>

</html>

What do you need help with exactly?

Your PHP code needs to be before the form is drawn, so that it runs first.

@droopsnoot @rpkamp

Here is my code, could you pls help me,

<!DOCTYPE HTML>
<html>
<body>
    <form action="#" method="post">
        Select Courier :
        <select name="courier">
            <option value="">--Please choose an option--</option>
            <option value="professional_courier">Professional Courier</option>
            <option value="india_post">India Post</option>
        </select>

        Trackingid: <input type="text" name="trackingid"><br>
        <input type="submit">

    </form>
    <?php
    // if (!empty($_POST)): header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0");
    // endif;

    if (!empty($_POST['courier']) && !empty($_POST['trackingid'])) {
        switch ($_POST['courier']) {
            case 'professional_courier':
                $url = 'https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0';
                break;
            case 'india_post':
                $url = 'https://www.indiapost.gov.in/_layouts/15/dop.portal.tracking/trackconsignment.aspx';
                break;
            default:
                throw new RuntimeException(sprintf('Unknown courier "%s"', $_POST['courier']));

                header('Location: ' . $url);
        };
    };
    ?>

</body>

</html>

@droopsnoot @rpkamp Following code working good and successfully redirect to professional courier tracking page :

<!DOCTYPE HTML>
<html>  
<body>

<form action="#" method="POST">
Select Courier :
<select name="courier">
  <option value="">--Please choose an option--</option>
  <option value="professional_courier">Professional Courier</option>
  <option value="india_post">India Post</option>
</select>

Trackingid: <input type="text" name="trackingid">
<input type="submit">

</form>



<?php

if (!empty($_POST)): header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0");


endif;

?>
</body>
</html>

How can i add more than one url combination, every courier tracking page url different right?

I said this before, yet you still have it after the form code.

In your code in post #13, you need the header redirect outside the switch block, but inside the if()` test.

Yes, you just have to manually find them and add them.

What is an error in below code,

<!DOCTYPE HTML>
<html>  
<body>

<form action="#" method="POST">
Select Courier :
<select name="courier">
  <option value="">--Please choose an option--</option>
  <option value="professional_courier">Professional Courier</option>
  <option value="india_post">India Post</option>
</select>

Trackingid: <input type="text" name="trackingid">
<input type="submit">

</form>

<?php
if(isset($_POST['courier']))
{
	if(isset($_POST['professional_courier']))
	{
		if (!empty($_POST)): header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0");
	}

	else(isset($_POST['india_post']))
	{
		if (!empty($_POST)): header("Location: https://www.dhl.com/en/express/tracking.html?AWB=".$_POST["trackingid"]."&brand=DHL");
	}	
}
?>
</body>
</html>

These are not form variables:

if(isset($_POST['professional_courier']))
...
else(isset($_POST['india_post']))

These are values within a different form variable, $_POST['courier'].

Look at the code that @rpkamp posted to see what you should be checking the value of. You can also var_dump($_POST); and see exactly what is coming back in from your form.

Your form processing code must be before you draw the form. In particular, your header redirect won’t work after you send anything at all to the browser, and you should get errors.

When your user selects “India post”, your code redirects them to DHL.

In this bit:

if(isset($_POST['professional_courier']))
	{
		if (!empty($_POST)): header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=".$_POST["trackingid"]."&type=0&service=0");
	}

what is the need for the innermost if clause? You’re inside a check to see if $_POST['courier'] is set, and if it is, then you already know that $_POST is not empty. I don’t know whether the colon after the if() is valid or not, I know there’s an alternate syntax and my initial question was going to be why you are mixing the two. But there’s no need at all for that if() as it will always return true if the code gets to that point. (Obviously that’s only the case once you’ve sorted out correctly checking for which option was selected - at the moment, as there’s no form variable called professional_courier, it won’t get there at all.)

There may be other errors in the code, have a stab at fixing the above first and come back if you need more assistance.

Finally working, thanks to everyone,

<!DOCTYPE HTML>
<html>

<body>

	<form action="#" method="POST">
		Select Courier :
		<select name="courier">
			<option value="">--Please choose an option--</option>
			<option value="professional_courier">Professional Courier</option>
			<option value="india_post">India Post</option>
		</select>

		Trackingid: <input type="text" name="trackingid">
		<input type="submit">

	</form>

	<?php
	if (isset($_POST['courier'])) {
		if ('professional_courier' === $_POST['courier']) {
			header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=" . $_POST["trackingid"] . "&type=0&service=0");
		} else if ('india_post' === $_POST['courier']) {
			header("Location: https://www.dhl.com/en/express/tracking.html?AWB=" . $_POST["trackingid"] . "&brand=DHL");
		}
	}
	?>
</body>

</html>

I’m surprised the redirect works.

1 Like

So am I. You should be receiving an error that the headers have already been sent.

Not if output buffering is enabled …

(not saying it’s a good idea, just saying it’s possible)