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