1

我正在尝试通过实际做一些我可能觉得对自己有用的事情来学习 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 } ?>

所以请告诉我是否有任何方法可以做我愿意做的事情。我会很高兴的。

如果要测试脚本,请使用以下值: week: 05 base: 16010234203 start check: 350900 end check: 350920 。

它将返回 19 张假票和 1 张真票。我希望所有这些显示的文本都导出到文本文件而不是显示在我的屏幕上。

有没有办法做到这一点?

先感谢您。

4

1 回答 1

0

设置CURLOPT_RETURNTRANSFERtrue,然后在你做的地方curl_exec(),将它分配给这样的变量:

$data = curl_exec($ch)

然后你可以像这样保存它:

file_put_contents('my_file.txt', $data);

使用常量而不是字符串文字来表示真假等。我在你的函数参数中看到你所做$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
}
于 2013-02-06T17:24:41.150 回答