Home > Article > Backend Development > PHP uses curl library to send HTTP requests
In web development, sending HTTP requests is a very important task. Whether you are getting data through an API or communicating with a third-party service, you need to use HTTP requests for data transfer. In PHP, you can use the curl library to send HTTP requests. This article will introduce the use of the curl library in detail.
1. Curl library introduction
The curl library is a library used for file transfer and supports multiple protocols. In addition to supporting common protocols such as HTTP, HTTPS, and FTP, it also supports email protocols such as SMTP and POP3, and file transfer protocols such as SCP and SFTP. The curl library is a very powerful tool that can help us quickly develop HTTP clients to realize data interaction with third-party services.
2. Basic usage of curl library
The curl library provides many functions. Here we only introduce some commonly used functions.
Sending a GET request using the curl library is very simple. You only need to call curl_init() to create a curl resource, and then set the URL and other parameters. Here is an example:
$url = 'http://example.com/api/getdata'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl);
The above code uses the curl_init() function to create a curl resource and set the requested URL. Then use the curl_setopt() function to set the CURLOPT_RETURNTRANSFER parameter, which indicates that the HTTP response will be output directly to a variable instead of displayed on the screen. Finally, the curl_exec() function is called and the result is stored. After execution, curl_close() is used to close the curl resource.
In addition to GET requests, it is also very simple to use the curl library to send POST requests. Just set the CURLOPT_POST parameter to 1 when calling the curl_setopt() function, and use curl_setopt() to set CURLOPT_POSTFIELDS to set the POST data.
The following is an example:
$url = 'http://example.com/api/postdata'; $data = array( 'name' => 'John', 'email' => 'john@example.com', 'message' => 'Hello World!' ); $curl = curl_init($url); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl);
The above code uses the http_build_query() function to convert POST data into URL-encoded data, and sets the CURLOPT_POST and CURLOPT_POSTFIELDS parameters.
In actual applications, we sometimes need to send data in JSON format to the server, which can be easily achieved using the curl library. Just set the CURLOPT_HTTPHEADER parameter to specify the request header information and send the JSON data as the request body.
The following is an example:
$url = 'http://example.com/api/sendjson'; $data = array( 'name' => 'John', 'email' => 'john@example.com', 'message' => 'Hello World!' ); $json = json_encode($data); $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($json)) ); $result = curl_exec($curl); curl_close($curl);
The above code calls the json_encode() function to convert the associative array into JSON format data, and sets request headers such as Content-Type and Content-Length. information.
3. Advanced usage of curl library
The curl library not only provides the basic function of sending HTTP requests, but also provides many advanced functions that can help us better control the request process. Here are some commonly used advanced functions.
Sometimes, we need to access network resources through a proxy server. It is also easy to set up a proxy server using the curl library. Just set the CURLOPT_PROXY parameter.
The following is an example:
$url = 'http://example.com/api/getdata'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_PROXY, 'http://proxy.example.com:8080'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl);
The above code calls the curl_setopt() function to set the CURLOPT_PROXY parameter and specifies the address and port of the proxy server.
Sometimes, network requests may cause the program to run for too long due to network delays or long server response times. To avoid this, we can set a timeout. The timeout can be easily set using the curl library, just set the CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT parameters.
The following is an example:
$url = 'http://example.com/api/getdata'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl);
The above code sets the CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT parameters, which represent the connection timeout and request timeout time respectively.
When communicating with a server using the HTTPS protocol, we need to authenticate the server, otherwise the communication will not be established. SSL verification can be very convenient using the curl library. You only need to set the CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST parameters.
The following is an example:
$url = 'https://example.com/api/getdata'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl);
The above code sets the CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST parameters, which respectively indicate whether to authenticate the server and verify whether the host name matches the certificate.
4. Summary
This article introduces the basic and advanced usage of using the curl library to send HTTP requests in PHP, including sending GET requests, POST requests, JSON data, setting agents, and setting timeouts. and set up SSL verification, etc. Using the curl library can help us implement the HTTP client more conveniently, thereby realizing the function of data interaction with third-party services.
The above is the detailed content of PHP uses curl library to send HTTP requests. For more information, please follow other related articles on the PHP Chinese website!