【问题标题】:Please help me save data sent by the server in response to my cURL GET request请帮我保存服务器响应我的 cURL GET 请求发送的数据
【发布时间】:2013-02-06 17:10:14
【问题描述】:

我正在尝试通过实际做一些我可能觉得对自己有用的事情来学习 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 个真票。我希望所有这些显示的文本都导出到文本文件中,而不是显示在我的屏幕上。

有没有办法做到这一点?

提前谢谢你。

【问题讨论】:

    标签: php html curl


    【解决方案1】:

    CURLOPT_RETURNTRANSFER 设置为true,然后在您执行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
    }
    

    【讨论】:

    • 非常感谢。它真的奏效了。我非常感谢你的帮助。祝你有美好的一天:)!
    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多