php - 使用curl加载使用JS提交外部表单的HTML文件

标签 php javascript curl

我需要一点帮助:

我有2个文件

  • index.php
  • form0.html

form0.html 自动填写表格并发送。 当我直接进行填充时,它工作正常,但是当我尝试通过我的 php 脚本访问它时,除非打印结果,否则它将无法工作。

PHP 代码:

<?php

set_time_limit(30);
$delay_time = 2; // time to wait before looping again.
$loop_times = 1; // number of times to loop.
$url = array("http://localhost/htmlfile0.html");

for($x=0;$x<$loop_times;$x++)
{
    echo count($url);
    for($i=0;$i<count($url);$i++)
    {
        $url1=$url[$i];
        $curl = curl_init(); // Initialize the cURL handler

        $header = array();
        $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
        $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 30000";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: "; // browsers keep this blank.
        $var_host = parse_url($url1,PHP_URL_HOST);


        $cookieJar = 'cookies/'.$var_host.'.txt'; // change it according to your requirement. make dynamic for multi URL
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // Browser like header
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieJar);  // file for cookies if site requires
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieJar); // file for cookies if site requires

        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9) Gecko/2008052906 Firefox/3.0');
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // Follow any redirects, just in case
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);

        curl_setopt($curl,CURLOPT_RETURNTRANSFER, true); // set curl to return the page

        curl_setopt($curl, CURLOPT_POSTFIELDS, '');
        curl_setopt($curl, CURLOPT_POST, true); //post the form

        curl_setopt($curl, CURLOPT_URL, $url1); // Set the URL
        $ch=curl_exec($curl); // Display page                  
        curl_close($ch); // Close cURL handler

        echo date('h:i:s') . "\n";
        if($ch) echo "Success: ".$url1;
        else echo "Fail: ".$url1;
        echo '<hr>';
        sleep(5);
        echo date('h:i:s') . "\n";
    }
    if($x < $loop_times) sleep($delay_time);
}
?>

我怎样才能通过这个?

谢谢。

最佳答案

如果我错了,请纠正我,但你试图使用 CURL 请求执行 JS,但你不能,至少不能直接执行 JS。

当你直接进入文件时,它工作正常 - 你的浏览器的 JS 解释器正确执行 JS 并提交表单,但是通过使用 cURL 你正在做一些不同的事情 - 你将 http 请求 header 发送到某些 URL(http://localhost/htmlfile0.html),您可能会也可能不会获取响应内容。

如果您确实获取内容(并在浏览器的 js 解释器中解析 JS),则 javascript 可能 设置为根据 URL 是否正确而拒绝正确的操作。

示例:

如果 URL 正确,则执行一些操作 - 如果您到达 http://example.com/script.html

上的代码

如果不在 URL 之上,请勿执行任何操作 - 当您使用 cURL php 脚本执行操作以到达上述 URL 并在 http://中输出文档代码时,就会出现这种情况/本地主机

关于php - 使用curl加载使用JS提交外部表单的HTML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13163382/

相关文章:

shell - 对于 shell 脚本中的 cURL,我在服务器上获取 http 代码为 000 或超时

php - 在 MySQL 数据库中保存数据的最有效方法是什么

php - 回显评论会返回所有注册用户的所需评论

php - 如何防止 PHP 中的 SQL 注入(inject)?

javascript - 如何获取特定元素的用户样式

ssl - curl 错误 35 : unknown SSL protocol error in connection

php - 如何使用 curl 和 PHP 设置自定义请求 header 键?

php - 并行执行多个 PHP 脚本,并在完成时收到通知

javascript - DataTables 条件格式栏

javascript - 在用户单击的位置插入图像