if(isset($_POST) && $_POST['btnSearch'] == 'Search'){
$success = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.somesite.com/tracking.php');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
//sometimes the page does not return information on the first attempt...so let's keep trying
$safety = 0;
while($success === false && $safety < 5){
$result = curl_exec($ch);
//result here means ANY RESPONSE. A result here does not mean a successful query.
if(! empty($result)){
$success = true;
}
$safety++;
}
curl_close($ch);
//pull the "mytable" table out of the HTML so we can display the results
$order_found = preg_match('/(<table id="mytable"[.\s\S]*?<\/table>)/i',$result, $matches);
//did we find any orders?
if($order_found){
//a bunch of crap was on this table so lets clean it up
$patterns = array();
$patterns[0] = '/<td align="left" width="16">[.\s\S]*?>/'; //removing empty td at start of table.
$patterns[1] = "/href='javascript.*?'/";
$patterns[2] = '/<a href="" >/';
$patterns[3] = '/align="center"/';
$clean_table = preg_replace($patterns, '', $matches[0]);
//replace the detail view link with a local page.
$clean_table = preg_replace('/ViewPackageDetail.aspx/', '/client-tools/view-package/', $clean_table);
//setup links to open in a new window
$clean_table = preg_replace('/<a href=/', '<a target="_blank" href=', $clean_table);
}else{
$clean_table = "<p>No packages met the search criteria.</p>";
}
}
//now just echo your table!
echo $clean_table;