How do I send data to a form using Curl?

Asked

Viewed 5,350 times

3

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

How could I do this in php?

2 answers

5


You can send a request using the library Curl this way: once defined a array of value key with its parameters:

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

You perform the requisition 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) on the variable $result.

  • Thank you for the reply ;)

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 warn the server that the date type is from a form
--data 'string' sends the data
-v verbose


In accordance with the php documentation about curl_ini() the most basic way to make 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);
?>
  • Thanks for the @Moshmage reply, but I needed a php example. (I had forgotten this detail in the question but already edited it)>

  • @GWER Edited :)

  • Thank you very much :P (+1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.