How to POST values using cURL request and get the response


Make your POST variable first:


$params = array(
 'first_name' => 'Ziyed',
 'last_name' => 'Uddin',
 'email' => 'test@email.com'	
);

Write a function to prepare your cURL post variable:


private function build_params($params) {
 $return = array();
 foreach ($params as $key => $value) {
  $return[] = $key . '=' . urlencode($value);
 }
 return implode('&', $return);
}

Now define your cURL connection, send the request and get the response


$ch = curl_init(); // Create a cURL instance
curl_setopt($ch, CURLOPT_URL, 'http://www.yourwebsiteurl.com'); //Set your cURL POST url
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->build_params($params)); //Prepare your cURL POST variable
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch); //Execute cURL Request and Get the Response
curl_close($ch); // Close cURL instance

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s