Curl is used to communicate with different servers. It also supports different protocols like http, https, ftp, telnet and file protocols for communication. Libcurl also supports HTTPS certificate. I already covered curl get request tutorial in my previous posts but in this tutorial I am going to show you how to use php curl post request with parameters.
PHP CURL POST:
Following are the CURL options we are going to use in curl post request. There are other lots of option which you can use with curl_setopt(). For more details visit php curl_setoptOption | Value |
---|---|
CURLOPT_URL | Request URL |
CURLOPT_HEADER | TRUE to include the header in the output. |
CURLOPT_RETURNTRANSFER | TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. |
CURLOPT_CONNECTTIMEOUT | The number of seconds to wait while trying to connect. Use 0 to wait indefinitely |
CURLOPT_POST | TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms. |
CURLOPT_POSTFIELDS | The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ‘;type=mimetype’. This parameter can either be passed as a urlencoded string like ‘para1=val1¶2=val2&…’ or as an array with the field name as key and field data as value. |
CURLOPT_SSL_VERIFYPEER | FALSE to stop cURL from verifying the peer’s certificate. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php $postDataArray = [ "first_name"=>"Ahsan", "last_name"=>"Zameer", "email"=>"ahsan@example.com", "phone"=>"+92123456789", ]; $data = http_build_query($postDataArray); $url = 'http://wdb24.com/curl-post-test.php'; $cURL = curl_init(); curl_setopt($cURL, CURLOPT_URL,$url); curl_setopt($cURL, CURLOPT_POSTFIELDS, $data); curl_setopt($cURL, CURLOPT_HEADER,false); curl_setopt($cURL, CURLOPT_RETURNTRANSFER,true); curl_setopt($cURL, CURLOPT_POST, true); curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT,10); $response = curl_exec($cURL); curl_close($cURL); echo $response; ?> |