How can I submit an input to another site form, and get results on same page?


#1

This post was flagged by the community and is temporarily hidden.


#2

Look into cURL

<?php

// check to see if the form was submitted.
if($_SERVER['REQUEST_METHOD'] == 'POST') {

    // do a cURL request and store the results in a variable


}


?>
<!-- Your form code -->
/..../
<!-- echo the variable hold the cURL results if it is set -->

#3

Thanks for the advice. I have been trying to play with curl a little since my post. Here is what I have come up with so far, but there is problems which I don’t yet know how to fix:

<?php

//$curl is going to be data type curl resource  
$curl = curl_init();

//url is equal to first url part A + $search_string + url part B
$url = "https://tptrack.info/ecom/dtrack.php?id=" + $search_string + "&type=a1";

//set our options 
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//defines what the $results are
$results = curl_exec($curl);

//if preg_match_all is equal to the table beginning then set it as $matches
preg_match_all("<table class='table table-striped' style='font-size: 16px;'>", $results, 
$matches);

//echo the $matches & add a line break
echo $matches + "<br />";

//close our curl
curl_close($curl);

?>

I am not sure if I can grab whatever is within the table tags this way, or if there is some other problem here.


#4

@ErnieAlex scraping is up your alley


#5

The post to page is messed up. ( https://tptrack.info/ecom/dtrack.php )
It has too many < HTML > tags in it and they are not in the correct places. To review pages to see how they are set up, you just RIGHT-CLICK on the page, not on a button, and select VIEW-SOURCE from the context menu. If you do this on that page, you will see it is all messed up. Lots of errors on it. Also, you will see that this page does not use a standard SUBMIT system. It uses Javascript. The JS handles submitting or posting the form.

Now, even though it has errors on it, it appears to work okay. I used tracking code " 1 " as a test on that page and it does pull up results. But, this just showed one button labeled “Transactions”. Pressing that showed the entire list for tracking code #1. To load a webpage in PHP, you can just use a simple one-line command. Something like this would work:

$tracking_code = 1;
$webpage=file_get_contents("https://tptrack.info/ecom/dtrack.php?id=" . $tracking_code . "&type=a1");

This would load the entire displayed page into a variable. Then, you would have to parse the data it loaded to pull out the data you need. In this case, for tracking code #1, it is shown in the resulting page as a table.
If you want the entire table, it is easy to do. I only see one table in the resulting page. Therefore, you can just search for the <table> tag and the </table> tag and pull out just that area and then you would have the entire table of live data. This can be done something like this:

$start = strpos($webpage, "<table>");
$end = strpos($webpage, "</table>");
$size_of_table = $end-start+7;   //  Length from start to end of the </table> tag...
$data_table = substr($webpage, $start, $size_of_table);

What this would do is take the webpage that was pulled with the results of the tracking code number and then pull out table which displays the data for that one tracking code. You would need to add in formatting code in your CSS to make it pretty. ( Please note I did not test this live, so it might need adjusting. )

Is that what you need? Or did you need the full details that are show on the Transactions button?


#6

Okay thanks. I have included a sample tracking code from a previous parcel to show some live data. And I am guessing I would just need to echo the $data_table afterwards like below?

$tracking_code = SRN011812110034;
$webpage=file_get_contents("https://tptrack.info/ecom/dtrack.php?id=" . $tracking_code . "&type=a1");
$start = strpos($webpage, "<table>");
$end = strpos($webpage, "</table>");
$size_of_table = $end-start+7;
$data_table = substr($webpage, $start, $size_of_table);

echo $data_table;

This works, but it grabs everything on the page, where as I only need the table. It includes the top menu links before the table for some reason instead of whats inside the <table> </table> tags. I have tried altering <table> to say <table class="table table-striped" style="font-size: 16px;"> but this displays nothing.
As you mentioned there is a page that gives some quick info, and then you have to click a Transaction button to view the full transaction summary on another page. The code above gives the quick summary, but I also need the transaction summary on the second page too.


#7

This is a very interesting problem. I have reviewed the live page and it is not coded correctly.
It seems that for some strange reason PHP is pulling in the wrong page data due to the errors on the page.
It appears that they created the page from two different sources and built the page wrong from the two other
pages. Not sure why they would do that at all.

I will have to experiment with it to see if I can solve it for you. Very busy today, but will attempt a fix for it
today at some point. Getting the transaction list would be another routine, but, we can solve that, too.
More in awhile…


#8

Dayley, try this version. It fixes the odd problem with that website’s page.
I also added in a second section to get the transactions as you mentioned at some point.
Hope this helps! Enjoy…

<?PHP

//  Part one of scraper, get tracking table
$tracking_code = SRN011812110034;
$webpage = file_get_contents("https://tptrack.info/ecom/dtrack.php?id=" . $tracking_code . "&type=a1");
$start = strpos($webpage, "<table");
$webpage = substr($webpage, $start);
$end = strpos($webpage, "<button");
$data_table = substr($webpage, 0, $end);

echo $data_table;

//  Part two of scraper, get the detailed transaction list
$trans_webpage = file_get_contents("https://tptrack.info/ecom/tdetail.php?fn=a1&code=" . $tracking_code);
$start = strpos($trans_webpage, "<div class='container marketing");
$end = strrpos($trans_webpage, "</div>");
$trans_data = substr($trans_webpage, $start, $end - $start);

echo "<br><br>" . $trans_data;

?>

#9

One more thought, this is the stripped out data. It does not contain the CSS code for making it fancy looking.
If you want that, you can just download their CSS file and us that so the display would match theirs 100%.
And, if you do not know how to do that, let us know…


#10

This works perfectly! Actually instead of copying their layout, I will try to grab each part of the data separately and place into my own layout/tables for consistency. I think this would be a cleaner option as this courier is not the only courier that we will be tracking, and I am certain other couriers will have other layouts (and problems).

I do have another question though… this courier’s output is in Thai, yet when it outputs to the page it looks like its in WindDings symbols/font. Is there a way to set the correct language, font, so that it displays correctly?


#11

Well, you can set the font in CSS or even in HTML. If you go to that page and RIGHT-CLICK,
then select VIEW-SOURCE, you can look at the source of the page. In there, you should see where
they set the the font and you can match it that way.


#12

Actually after changing my CSS to font-family:Helvetica Neue,Helvetica,Arial,sans-serif same as the couriers font, this doesn’t make a difference. If I use the same font on my page the output remains the same. I am assuming that our Javascript is grabbing the Thai as symbols, outputting it as symbols, and then we are only changing the font-family of the symbols.


#13

Set the page character encoding to UTF8 and the language to th as follows.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="th">

#14

This post was flagged by the community and is temporarily hidden.


#15

Hmmm, it is very odd! Looking at the original site with the Thai language showing, I viewed the source and it shows the Thai words exactly as it should. It appears that it is the way that the file-get-contents works. I found hundreds of fixes online so far. Tried a lot of them and some almost worked. It is not the displayed parts, but, the incoming encoding that is the issue. I will find a solution for you, hopefully today…


#16

SOLVED IT! I sure love a good puzzle…

Well, it seems that the page on their site is using character-encoding of “windows-874”.
This is a common encoding version for Thai and other languages. But, it is not UTF-8 or other formats.
Therefore, I had to convert it from one to the other. After that, the display matches the original page better!
Here is a new test page for you to try:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="Content-Language" content="th">
    </head>
<body>
<?PHP
//  Part one of scraper, get tracking table
$tracking_code = SRN011812110034;
$webpage = file_get_contents("https://tptrack.info/ecom/dtrack.php?id=" . $tracking_code . "&type=a1");
$webpage = iconv('windows-874', 'utf-8', $webpage);
$start = strpos($webpage, "<table");
$webpage = substr($webpage, $start);
$end = strpos($webpage, "<button");
$data_table = substr($webpage, 0, $end);

echo $data_table;

//  Part two of scraper, get the detailed transaction list
$trans_webpage = file_get_contents("https://tptrack.info/ecom/tdetail.php?fn=a1&code=" . $tracking_code);
$trans_webpage = iconv('windows-874', 'utf-8', $trans_webpage);
$start = strpos($trans_webpage, "<div class='container marketing");
$end = strrpos($trans_webpage, "</div>");
$trans_data = substr($trans_webpage, $start, $end - $start);

echo "<br><br>" . $trans_data;

?>
    </body>
</html>

This one pulls out just that one sample account number you posted. You can see where I had to add in
one new line for each section using the “iconv()” function. I think this will finish it up for you…


#17

Great work! This is now working.
My last couple of problem are:

1. The signature images. They use http, but when copied they don’t display as they are trying to use https.

2. I wish to use an input box and button, so that the tracking code is a variable that calls a Shortcode (Wordpress won’t allow me to type php direct on a Wordpress page so it is done via the php allowing plugin PHP code snippets (Insert PHP) by Will Bontrager Software. The Shortcode is the php for grabbing the tracking). Although not working, I am attempting to do it like this:

<h2>Track with Thai Parcels</h2>
<div class="input-append">
    <input name="ThaiParcelsCode" id="ThaiParcelsCode"/>
    <button class="btn ThaiParcelsButton" onclick="grabThaiParcelsTracking()">Track Parcel</button>
</div>

Here’s the code for the Javascript inside the button:

<script>
function grabThaiParcelsTracking() {
do_shortcode('[wbcr_php_snippet id="129728"]');
}
</script>

And finally our code for the PHP we have been using:

//  Part one of scraper, get tracking table
$tracking_code = $_POST['ThaiParcelsCode'];
$webpage = file_get_contents("https://tptrack.info/ecom/dtrack.php?id=" . $tracking_code . "&type=a1");
$webpage = iconv('windows-874', 'utf-8', $webpage);
$start = strpos($webpage, "<table");
$webpage = substr($webpage, $start);
$end = strpos($webpage, "<button");
$data_table = substr($webpage, 0, $end);

echo $data_table;

//  Part two of scraper, get the detailed transaction list
$trans_webpage = file_get_contents("https://tptrack.info/ecom/tdetail.php?fn=a1&code=" . $tracking_code);
$trans_webpage = iconv('windows-874', 'utf-8', $trans_webpage);
$start = strpos($trans_webpage, "<div class='container marketing");
$end = strrpos($trans_webpage, "</div>");
$trans_data = substr($trans_webpage, $start, $end - $start);

echo "<br><br>" . $trans_data;

If you know of a much better/easier way to do this, or can provide any help in finishing this off would be greatly appreciated!


#18

1 - not sure what you mean. My last version shows it just fine when it echo’s the $trans_data.
It shows up at the bottom of the display? Do you mean it is not showing on your WP version?

2 - Well, to get the data pulled in from an external PHP file, you would have to do this in an entirely different way and code it to get the typed in value and then send it to the PHP code. Since PHP is run server-side, you can NOT just use Javascript to do this as the PHP code must run BEFORE the Javascipt can even know the page exists.

First to explain that further. How webpages work is that the PHP is processed on the server when a page is requested. Once PHP is done, the OUTPUT of the PHP code along with the HTML is sent to the browser. Then, the browser takes over and “renders” the HTML along with handling the CSS code. That is pushed out so the user can see it. At that point, Javascript becomes available in the browser as a CLIENT-SIDE programming scheme.

So, what you would need is to use AJAX. Basically, you would need to get the ID number from the input field using Javascript. Then, process an AJAX call out to the outside PHP program in it’s own file. This would need to pull out the ID number from the call and process it, sending back the data. All a bit tricky.

Step one is to get the Javascript to pull in the value in the input field. You would need to change your script to do it something like this:

<script>
function grabThaiParcelsTracking() {
     var idCode = document.getElementById('ThaiParcelsCode').value;
     **** Here run AJAX call ***
}
</script>

Step two, you would need to create an AJAX call. To do this, you need to alter the previous version of PHP code to grab (GET) the ID number passed to it. This is easy. Take this code and save it as a file named “getParcels.php”.

<?PHP

//  Get the parcel ID number passed from the AJAX call
$tracking_code = $_GET['ThaiParchelsCode'];

//  Part one of scraper, get tracking table
$webpage = file_get_contents("https://tptrack.info/ecom/dtrack.php?id=" . $tracking_code . "&type=a1");
$webpage = iconv('windows-874', 'utf-8', $webpage);
$start = strpos($webpage, "<table");
$webpage = substr($webpage, $start);
$end = strpos($webpage, "<button");
$data_table = substr($webpage, 0, $end);

echo $data_table;

//  Part two of scraper, get the detailed transaction list
$trans_webpage = file_get_contents("https://tptrack.info/ecom/tdetail.php?fn=a1&code=" . $tracking_code);
$trans_webpage = iconv('windows-874', 'utf-8', $trans_webpage);
$start = strpos($trans_webpage, "<div class='container marketing");
$end = strrpos($trans_webpage, "</div>");
$trans_data = substr($trans_webpage, $start, $end - $start);

echo "<br><br>" . $trans_data;

?>

As you see, it is nearly the same except it pulls in the tracking_code from a GET not a POST. This is a very important difference since we are not POSTING to this page. If you wanted to add in some safety coding, you could check for invalid codes or no code at all being entered. You can not test it the previous way by just going to the page. Instead you would need to add the parameter at the end. Test this page using this type of url:
`

getParcels.php?ThaiParchelsCode=SRN011812110034

That would show the results for the text ID we have been using. Now to continue with the AJAX part, you need to create a call to this new page inside the Javascript code. This would be done something like this:

<script>
function grabThaiParcelsTracking() {
     var idCode = document.getElementById('ThaiParcelsCode').value;
     $.ajax( {
          url: 'getParcels.php',
          type: 'POST',
          data: 'ThaiParchelsCode='+idCode,
          async: true,
          success: function (data) {
              alert(data)
          },
    });
}
</script>

This would POP-UP the data you selected. It basically makes a Javascript “ALERT” pop out with the returned data in the alert. We can change that to save the data inside a <DIV tag or other forms fields.
To save the data inside of a <DIV tag, you just need to assign a name to the tag and replace the alert
with a save command. Let’s say you have this DIV in the page:

 <div id='displayParcels'>...</div>

You need the id so that Javascript and find it and load it up. To that, you would replace the alert line with:

          var div = document.getElementById('displayParcels');
          div.innerHTML = data;

This would locate the DIV and store the data retrieved from the getParcels.php page and it would show up automatically. The rest of the page will not be altered at all, so it should smoothly show up the data.

I hope this is what you wanted. Hope it helps!


#19

Sorry for the long post, but, I thought you needed a lot more explanations on how all that works.


#20

This post was flagged by the community and is temporarily hidden.