How to send data to a form using cURL?

3

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

How could I do this in php?

    
asked by anonymous 24.04.2015 / 15:22

2 answers

5

You can submit a request using the cURL library this way: once a array of key-value with its parameters:

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

You perform the request for $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 .

    
24.04.2015 / 15:48
3

cURL 'http://website.dom' -X POST -H 'Content-Type: application/x-www-form-urlencoded' --data 'data=valor&data2=valor2' -v

cURL the command itself. 'http://website.dom' the site to / path to where the POST goes -X POST tells cURL to use POST -H 'Content-Type: application/x-www-form-urlencoded' puts an HTTP Header to notify the server that the date type is of a form
--data 'string' sends the data
-v verbose

According to the php documentation on curl_ini () the most basic form of making a cURL in php is as follows:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>
    
24.04.2015 / 15:34