I am trying learn PHP by actually doing something I may find useful for myself. There is a betting agency in my country which has a website where I can check if I won or not by entering a ticket number. I am trying to write a PHP script to check if range of tickets are worth anything so I don’t have to enter every ticket manually on their website.
I managed to do that. But now I want to save the response I get from their server in a file. I run into serious problems with this. I managed to save the verbose into a file but I am unable to make the script save to a file what I see on the screen inside my browser after running the script.
Here is the code:
<?php
function check($week, $base, $startcheck, $endcheck, $verbose = "true"){
// Set file path
$verbosePath = 'publicbet.txt';
echo "Saving the tickets to: <b>$verbosePath</b>\n";
// Initiate numbering
$i = 0;
// Initiate publicbet.ro IP
$ip = "80.86.107.93";
// Loop
while ($startcheck <= $endcheck){
// Generate tickkey
$tickkey = $week.$base.$startcheck;
// get the current server time
$time = date('d-m-Y H:i:s');
// Open a new cURL resource
$ch = curl_init();
// Stuff
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_STDERR,$f = fopen($verbosePath, "a"));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0");
// publicbet.ro may be checking the IP adress
// from where the $tickkey is sent in order to
// check for abnormalities; we will send the
// IP adress of the website:)
$headerarray = array(
"X-Forwarded-For: $ip");
// Set the URL and other options
curl_setopt($ch, CURLOPT_URL, 'http://publicbet.ro/gettickinfo2.php?lang=ro&tickkey='.$tickkey);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.publicbet.ro/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
// Executing cURL
curl_exec($ch);
// Close cURL resource, free up system resources
curl_close($ch);
fclose($f);
// Showing informtion
$v .= "<br />$i. At <b>$time</b> the server checked the tickkey: <b>$tickkey</b> and returned the tickket: ";
if ($verbose == "true"){
echo $v;
$v = '';
}
// Modifying values
$startcheck++;
$i++;
}
}
if ($_POST[week] && $_POST[base] && $_POST[startcheck] && $_POST[endcheck]){
check($_POST[week], $_POST[base], $_POST[startcheck], $_POST[endcheck]);
}
else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>publicbet.ro</title>
</head>
<body>
<h1>Check your tickets here</h1>
<form action="<?php echo $_SERVER[PHP_SELF];?>" method="post">
<table>
<tbody>
<tr>
<td>week:</td>
<td>base:</td>
<td>start check:</td>
<td>end check:</td>
</tr>
<tr>
<td><input type="number" name="week" min="00" max="54" maxlength="2" size="2" value=""/></td>
<td><input type="number" name="base" maxlength="11" size="11" value=""/></td>
<td><input type="number" name="startcheck" maxlength="6" size="6" value=""/></td>
<td><input type="number" name="endcheck" maxlength="6" size="6" value=""/></td>
</tr>
</tbody>
</table>
<br /><input type="submit" value="Check!" />
</form>
</body>
</html>
<?php } ?>
So please tell me if there is any method of doing what I am willing to. I would be really pleased.
If you want to test the script use these values:
week: 05
base: 16010234203
start check: 350900
end check: 350920 .
It will return 19 false tickets and 1 true. I want all this text which is showing up to be exported to a text file rather than showing on my screen.
Is there a way to do this?
Thank you in advance.
Set
CURLOPT_RETURNTRANSFERtotrue, and then where you docurl_exec(), assign it to a variable like this:Then you can save it like this:
Use constants instead of string literals for things like true and false etc.. I see in your function arguments you do
$verbose = "true", this could be replaced with$verbose = true.You also are using constants where you should actually be using string literals, for example
$_POST[base]should be$_POST['base']or$_POST["base"]Please don’t use
$_SERVER['PHP_SELF']like that, it leaves you very open to XSS attacks, etc. You can doaction=""and it will post to the page in question, likePHP_SELFWhere you are doing
I recommend doing this instead: