php – How to submit data to a form using cURL?

Question:

I was reading about cURL, and I saw that I can use it to submit data to a form (as if I were typing the data and submitting).

How could I do this in php?

Answer:

You can send a request using the cURL library like this: once you define a key-value array with its parameters:

$fields = array('foo' => 'bar');

You make the request for the $url :

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

$result = curl_exec($ch);

curl_close($ch);

And get the result (if any) in the variable $result .

Scroll to Top