Friday, August 29, 2014

Curl with Form Data

Use Curl's -d switch to incorporate form data with a post.

curl -X POST http://localhost:4567/adduser -d first="Greg" -d last="Helton"
PHP has its curl method that can be used as follows:
$url = "http://localhost:4567/adduser";
$data = array("first" => "Greg", "last" => "Helton");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/json')
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);