PHP CURL

PHP CURL

What is PHP curl

cURL ( Client URL library ) is a way you can hit a URL from your code to get a html response from it. cURL means client URL which allows you to connect with other URLs and use their responses in your code.
Using CURL we can communicate with different type of servers with protocols FTP, HTTP, telnet, gopher, POST and GET form posting, SSL support, session, cookie handling and more.

PHP supports it through libcurl. To enable support for libcurl when installing PHP add –with-curl=[location of curl libraries] to the configure statement before compiling. The curl package must be installed prior to installing PHP.

To start a curl session use the curl_init() function. Options for the curl session are set via the curl_setopt() PHP function. Once you have the options set execute the request with the curl_exec() function.

cURL Supported Operations

The cURL as an conmanly is used for –
1. Form submitting
2. Authentication
3. File upload
4. File transfer

cURL Option Constants


    CURLOPT_FILE – target file to write cURL result.
    CURLOPT_URL – URL from where we need to get data.
    CURLOPT_RETURNTRANSFER – CURL return result in string format into a variable instead of printing it to the browser.
    CURLOPT_HTTPHEADER – to set header fields as an array.
    CURLOPT_POST – set to TRUE to perform HTTP POST.
    CURLOPT_USERPWD – to set username and password if required to connect your remote server.

PHP cURL Request

The both GET and POST methods are used for sending cURL request where GET is default. The CURLOPT_POST constant is used for sending POST request. And the parameters CURLOPT_POSTFIELDS is for sending parameter in array such as field:value pairs.

PHP CURL Example


< ?php
$postData = array("site" => "SoundwebTech- Web Designing and developing Experts", "dailyuser" => "1000", "location" => "India");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.soundwebtech.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.