简体   繁体   English

在加载的DOMDocument或curl中提交POST数据,并使用相同值的随机隐藏字段值

[英]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. 我宁愿使用cURL来执行此操作,但是在提取隐藏字段的值来提交帖子数据时似乎遇到了麻烦。 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 ... 尝试使用DOMDocument时,使用以下代码返回所需的值...

$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. ^这会返回值,但是在cURL中使用该值时似乎无效,因为cURL需要发布时给出的正确值,并且每次加载页面时该值都会改变。 My question is, is there any way to submit post data with the loadHTMLFile document with DOM or by some other means?? 我的问题是,有什么方法可以通过带有DOM或其​​他方式的loadHTMLFile文档提交帖子数据? 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?? 有没有一种方法可以保存给定的文件并将其放在CURLOPT_URL中,还是不能那样工作? 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?? 也许值必须从CURLOPT_POSTFIELDS进行的同一调用中派生,如果是的话,执行此操作的正确方法是什么? I'm a little lost as far as this is concerned and any direction will be greatly appreciated. 就此而言,我有点迷茫,任何方向都将不胜感激。

Your url has https:// in it. 您的网址中带有https:// Which means it requires ssl things to do. 这意味着它需要SSL来完成。 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). 也从卷曲中删除此行(如果仅执行GET请求)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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