简体   繁体   中英

Submitting POST data in loaded DOMDocument or curl with same value of random hidden field value

There are two hidden fields that are needed to submit a form. .... = random string;

This form needs to be submitted remotely. I would prefer to do this with cURL but I seem to be having trouble with pulling the value of the hidden fields to submit the post data. I've tried numerous things, such as;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://example.com/");
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, true);
//    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$curl=curl_exec($ch);

$slash = preg_match('/<input type="hidden" id="CSRFNAME" value="(.*?)"/',$curl, $batch);
$dash = preg_match('/<input type="hidden" id="CSRFTOKEN" value="(.*?)"/',$curl, $match);
//echo $batch."-".$match;
var_dump($batch);
echo "=";
var_dump($match);

^ This ^ returned no value. This was also tried ...

$bling = preg_match_all('/<input type="hidden"\s*id="([^"]*)"\s*value="([^"]*)"/i',
$curl,$matches);
//var_dump($matches);

^ This ^ returned no value as well.

When DOMDocument was tried the value that's needed was returned with the following code ...

$doc->loadHTMLFile('http://www.example.com/');
$CSRFN = $doc->getElementById('CSRFNAME')->getAttribute('value');
$CSRFT = $doc->getElementById('CSRFTOKEN')->getAttribute('value');
echo $CSRFN;
echo "=";
echo $CSRFT;

^ This returns the value but when using the value in cURL it seems that it doesn't work because cURL needs the proper value that's given when the post is made and the value changes every time the page is loaded. My question is, is there any way to submit post data with the loadHTMLFile document with DOM or by some other means?? Is there a way I can save the given file and put it in the CURLOPT_URL or does it just not work like that?? maybe the value has to be derived from the same call that CURLOPT_POSTFIELDS makes, if so, what's the proper way to do this?? I'm a little lost as far as this is concerned and any direction will be greatly appreciated.

Your url has https:// in it. Which means it requires ssl things to do. So use this code to handle this.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

Remove this line also from your curl(if you are doing only GET request).

curl_setopt($ch, CURLOPT_POST, true);

Further more, you can check your curl action by enabling the verbose option.

curl_setopt($ch, CURLOPT_VERBOSE, true);

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