我正在尝试通过实际做一些我可能觉得对自己有用的事情来学习PHP。我所在的国家有一家博彩公司,它有一个网站,我可以通过输入彩票号码来检查我是否中奖。我正在尝试编写一个PHP脚本来检查一系列的门票是否有价值,这样我就不必在他们的网站上手动输入每张门票。
我设法做到了。但现在我想把我从他们的服务器上得到的响应保存在一个文件中。我遇到了严重的问题。我设法将详细信息保存到一个文件中,但无法将脚本保存到运行脚本后在浏览器屏幕上看到的文件中。
这是代码:
<?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 } ?>
所以请告诉我是否有什么方法可以做我愿意做的事。我会非常高兴的。
如果要测试脚本,请使用以下值:周:05基数:16010234203启动检查:350900末端检查:350920。
它将退回19张假票和1张真票。我希望所有显示的文本都导出到文本文件中,而不是显示在屏幕上。
有办法做到这一点吗?
提前谢谢。
将CURLOPT_RETURNTRANSFER
设置为true
,然后在执行curl_exec()
时,将其分配给如下变量:
$data = curl_exec($ch)
然后你可以这样保存:
file_put_contents('my_file.txt', $data);
对于true和false等,使用常量而不是字符串文字。我在函数参数中看到你使用$verbose = "true"
,这可以用$verbose = true
代替。
您还使用了实际应该使用字符串文字的常量,例如$_POST[base]
应该是$_POST['base']
或$_POST["base"]
请不要那样使用$_SERVER['PHP_SELF']
,它会让你很容易受到XSS攻击等。你可以使用action=""
,它会发布到有问题的页面,比如PHP_SELF
你在哪里做
if($_POST[base] .....)
我建议这样做:
if(isset($_POST['base'], $_POST['somethingElse'], $_POST['anotherField'])) {
// yay
}