Friday, June 27, 2014

Using curl to perform a POST request in PHP

Just use this simple function to perform a POST request :

function do_post($url, $data)
{
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($ch);
  return $response;
}

$url is the website url for the request.
$data is an array that contains the form data that you want to send to the server. For example,
$data = array('name'=>'Bui Hoang Hai', 'website'=>'http://www.romajidesu.com')