Home >Backend Development >PHP Tutorial >Introduction to the difference between curl and libcurl, introduction to curllibcurl_PHP tutorial
Introduction to curl
curl is an open source file transfer tool that uses URL syntax to work in command line mode.
It supports many protocols: DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP.
Curl also supports SSL certificates, HTTP POST, HTTP PUT, FTP upload, form-based HTTP upload, proxies, cookies, username/password authentication (Basic, Digest, NTLM, etc.), download file breakpoint resume, upload File transfer resume, http proxy tunneling and other features.
Curl was developed by the Swedish curl organization. The official website of curl is http://curl.haxx.se/. Its source code and related instructions can be obtained from the official website.
Introduction to libcurl
libcurl is a free and open source client-side URL transmission library that supports DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP , SMTP, SMTPS, Telnet and TFTP and other protocols.
Like curl, libcurl also supports SSL certificates, HTTP POST, HTTP PUT, FTP upload, form-based HTTP upload, proxies, cookies, username/password authentication (Basic, Digest, NTLM, etc.), download file breakpoints Resume transfer, upload file breakpoint resume (file transfer resume), http proxy server pipeline (proxy tunneling), etc.
libcurl is highly portable and can work on different platforms, supporting Windows, Unix, Linux, etc.
libcurl is free, thread-safe, IPV6-compatible, and has many other very rich features. libcurl has been adopted by many well-known large enterprises and applications.
The source code and related documentation of libcurl can be obtained here.
Comparison between curl and libcurl
Sameness
Both curl and libcurl can use a variety of protocols to transfer files, including HTTP, HTTPS, FTP, FTPS, GOPHER, LDAP, DICT, TELNET and FILE, etc.
Differences
curl is a command line tool that can be run through shell or script. The underlying library used by curl is libcurl.
libcurl is a library that is usually bundled with other programs. For example, the command line tool curl encapsulates the libcurl library. So we can also use libcurl in your own program or project to get powerful features like CURL. The PHP extension to be introduced next is an encapsulation of curl.
A few nouns
Different meanings of “curl”
1.curl refers to the curl command line tool, which can run curl from the command line or script or batch file. Curl was created in 1998 and provides over 100 options to control it.
2.cURL is the name of a software project. This software project includes curl and libcurl mentioned above, and both are open source.
3.CURL is often used as the name of the libcurl extension in PHP. This extension ensures that PHP programmers can access the functionality provided by the libcurl library in their programs.
curl—command line tool
1. Command line tool, which can be run from shell or script.
2. Provides more than 130 different "flags"
3. Usually used to simulate browser behavior
4. Cross-platform
libcurl—library
1. Used as a development library for other programs
2. Can be combined with many languages, such as PHP, C
3. Cross-platform
4. Provides a variety of different APIs for using it
Using curl and libcurl in PHP
Using curl in PHP
Using curl in PHP is very simple, just call several related functions in PHP that execute system commands.
Code example:
Copy code The code is as follows:
$baidu=shell_exec("/usr/bin/curl -L http://www.baidu.com");
var_dump($baidu);
Using libcurl in PHP
Use libcurl in PHP, which is what we usually call "curl" in PHP. This part of the content will be given in a future article. Here is just a sample code:
Copy code The code is as follows:
// FTP this script to a server
$fp = fopen(__FILE__, "r");
$url = "ftp://username:password@mydomain.com:21/path/to/newfile.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_FTPASCII, 1);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize(__FILE__));
$result = curl_exec($ch);
curl_close($ch);
Use curl or libcurl?
Whether curl or libcurl should be used depends on the specific situation. For example, we should use curl when there is a scheduled script that sends an email when a file on the remote server changes or the current PHP environment does not support libcurl. Otherwise, we can use libcurl.