What is curl in php?

What is curl in php?

January 28, 2020

cURL is a command-line tool for getting or sending data including files using URL syntax. cURL is a computer software project providing a library (libcurl) and command-line tool (curl) for transferring data using various network protocols. The name stands for “Client URL”.

It supports a range of common network protocols, currently including HTTP, HTTPS, SOAP requests, FTP, FTPS, SCP, SFTP, TFTP, LDAP, DAP, DICT, TELNET, FILE, IMAP, POP3, SMTP and RTSP.

Examples

Basic use of cURL involves simply typing curl at the command line, followed by the URL of the output to retrieve:

curl https://statelyworld.com

cURL defaults to displaying the output it retrieves to the standard output specified on the system (usually the terminal window). So running the command above would, on most systems, display the https://statelyworld.com source-code in the terminal window. The -o flag can be used to store the output in a file instead:

curl -o ChessBoard.php https://statelyworld.com/projects/demos/ChessBoard.php

PHP: libcurl

PHP supports libcurl, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the HTTP, https, FTP, gopher, telnet, DICT, file, and LDAP protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s FTP extension), HTTP form based upload, proxies, cookies, and user+password authentication.

Example:

<?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "https://statelyworld.com/");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);     
?>