Tutorial Article on How to use POST method/request in PHP CURL, Execute HTTP POST method in PHP CURL

PHP Curl is a powerful tool provided to the community as a souvenir.

In simple words Curl is basically used to communicate with remote host, send a request and get a response. The response may contain data like a message, error response etc.

So its a very simple process. There are certain Curl commands through which you execute your request.

What you need?

You require a Url of the remote host, some variable to Post a request (like username/password) and finally execute the command to get a response.

Salesforce.com developers
Also to note that developers who want to do a Salesforce.com integration can use the script provided in this article.
The clients always has a requirement that the contact/lead form should be posted to salesforce.com database and an email should also be fired to the client.

Now lets talk about a real life example-

  1. You have a form which accepts certain user information like name, title, email, address etc.
  2. These form values are required to be posted to a remote host URL.
  3. The request should be secured and hence required to send the values using HTTP POST method.

Following is the code to resolve the above problem.

Click here to view this in action

<?php

//URL to Post request
$postUrl = 'http://www.example.com/get-request.php';

//Prepare the variable to post through PHP Curl
$postFields = array(
'name'=>urlencode($_POST["contactName"]),
'title'=>urlencode($_POST["title"]),
'company'=>urlencode($_POST["company"]),
'email'=>urlencode($_POST["email"]),
'phone'=>urlencode($_POST["phone"])
);

//url-ify the data for the POST
foreach($postFields as $key=>$val)
{
$postString .= $key.'='.$val.'&';
}
rtrim($postString,'&');

//create a new Curl connection
$ch = curl_init();

//set the remote host Url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$postUrl);
curl_setopt($ch,CURLOPT_POST,count($postFields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$postString);

//execute post request
$response = curl_exec($ch);

//close the connection
curl_close($ch);

//Now $response will contain data being sent back by the remote host this could be a message or a error code etc..

?>
Bookmark and Share
Both comments and pings are currently closed.

2 Responses to “Tutorial Article on How to use POST method/request in PHP CURL, Execute HTTP POST method in PHP CURL”

  1. Jene Ackers says:

    Very interesting post – may be old, however it was new to me. I wanted say thanks for this great post UncleCode.Com – Technology Blog | How to execute HTTP POST request / method using PHP CURL, POST form values using PHP CURL ! I undoubtedly loved reading every little bit of it. I have you bookmarked to check out new stuff you publish.

  2. Reading this article UncleCode.Com – Technology Blog | How to execute HTTP POST request / method using PHP CURL, POST form values using PHP CURL reminds me of my previous roommate! He always kept talking about this. I will forward this post to him. Pretty certain he will have an excellent read. Thanks for the information you publish.

n