cURL不工作与GET var和fopen - PHP


cURL not working with GET var and fopen - PHP

我在浏览器中打开test1.php,它应该在url中使用GET变量id调用test2.php,这反过来应该将id和时间附加到名为test1.txt的本地文本文件

但是行不通。

如果我用GET变量直接调用test2.php,它工作得很好- http://www.example.com/test2.php?id=123456 -但当我在浏览器中加载test1.php时它不工作。

test1.php

curl_setopt_array(
$ch, array(
CURLOPT_URL => 'http://www.example.com/test2.php?id=123456',
CURLOPT_RETURNTRANSFER => 0
));
curl_exec($ch);

test2.php

if(!isset($_GET['id'])) {
  die();
}
$ID = (int)$_GET['id'];
$myfile = fopen("test1.txt", "a") or die();
$txt = $ID . " - " .time() . "'n";
fwrite($myfile, $txt);
fclose($myfile);

php中是否有安全措施来防止这种行为,还是我在这里错过了一些非常明显的东西?

您需要初始化$ch:

$ch = curl_init();
url_setopt_array(
    $ch, array(
        CURLOPT_URL => 'http://www.example.com/test2.php?id=123456',
        CURLOPT_RETURNTRANSFER => 0
));
curl_exec($ch);

如果我没有错,您应该调用test2.php,但在代码中我看到您正在调用相同的文件。

    curl_setopt_array(
    $ch, array(
    CURLOPT_URL => 'http://www.example.com/test2.php?id=123456',
    CURLOPT_RETURNTRANSFER => 0
    ));