简体   繁体   中英

Using curl to load an HTML file that submit an external form with JS

I need a little help here:

I have 2 files

  • index.php
  • form0.html

form0.html automatically fill a form and send it. When I go straight to the fill it works fine, but when I try to access it through my php script it won't work unless I print the results.

PHP CODE:

<?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);
}
?>

How can I get pass this?

Thanks.

Correct me if I'm wrong but you're trying to execute JS using CURL request which you cant' at least not directly.

When you go straight to file, it works fine - your browser's JS interpreter executes JS proprerly and form is submitted, but by using cURL you are doing something different - you are sending http request headers to certain URL(http://localhost/htmlfile0.html) and you may and may not fetch the response content.

If you do fetch the content (and parse JS in your browser's js interpreter) javascript may be set to refuse correct action based on wheather it is or it is not correct url.

Example:

do some action if it is right URL - if you reached code on http://example.com/script.html

do not do anything if it's not above URL - and that's the case when you perform action using your cURL php script to reach above URL with and to output document's code in http://localhost .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM