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;
?>

Leave a Reply

Related Posts: You may like

How to remove whitespace from string using php

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

Convert Base64 string to an image file

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.