简体   繁体   中英

Send curl request with form values using PHP

I have a page like this:

<form action method="POST">
   <input type="hidden" name="rand" value="casualnumbers">
   <input type="submit" value="Submit">
</form>

I need to get the result page, sending with CURL the "rand" value as POST variable.

I know how to send POST variables using:

CURLOPT_POSTFIELDS => array("rand" => "casualnumbers")

But how can I take the "casualnumbers" from the form page?

The page generates a random value and the post request works only if it gets the right random value, so I need to take the random value from the page and immediately send the post request with the right random value

That's what I use:

 preg_match_all('/<input.*type="(submit|hidden)".*name="([^"]+)".*value="([^"]*)".*>/u',$form_html,$vars);
$post_vars=array();
for($i=0;$i<count($vars[2]);$i++)
    $post_vars[$vars[2][$i]]=$vars[3][$i];
curl_setopt($curl,CURLOPT_POST, true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post_vars);

Hope it helps.

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