web123456

Independent Developer Series (43)——Detailed explanation of CURL function library

In PHP development, CURL (Client URLLibrary) is a very powerful and commonly used tool that allows us to interact with various network resources such as sendingHTTP Request, obtain web page content, upload files, etc. This article will introduce in detail the detailed usage of the CURL library in PHP. It has the same function as the library in python. Here we mainly use PHP as the carrier for comprehensive summary and use. This library is used very frequently in programming. In basic applications, in addition to interaction with MYSQL, it is also the acquisition of remote APIs, which can be understood as the second library used. PHP without mysql is basically a waste, and the same is true without CURL

$curl = curl_init();  //Initialize the resource
  • 1

Here we understand the definition concept of resources in PHP: In PHP, "resource" is a special data type. Resources are data created and managed by external programs (such as database connections, file handles, image operation handles, etc.). A resource is not a specific data value like integers, strings, arrays, etc., but a reference or identifier pointing to an external resource. For example, usefopen() functionWhen opening a file, it will return a file resource, which can be used to perform subsequent file reading, writing and other operations. For example, when connecting to the database, theDatabase connection objectIt is also a resource. Resources usually need to be operated and released through specific functions to avoid resource leakage. When no longer need to use resources, the corresponding function should be used (e.g.fclose()Close file resources, close database connections, etc.) to free up resources.
Resources are types defined like int boolen float object. In common programming, there is rarely a concept of resource type, and curl_init is to open an initialized resource.

Configuration of request parameters

curl_setopt($curl, CURLOPT_URL, ""); //Link
curl_setopt($curl, CURLOPT_POST, true); // Send post request. If you don’t set the default, just get it directly
$data = ['key1' => 'value1', 'key2' => 'value2'];  //Related data
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));//If it is post method, send data. This is the requested parameter
 $headers = ['Content-Type: application/json'];
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //Set request header information, which is what we usually call request header
 curl_setopt($curl, CURLOPT_TIMEOUT, 10); //Request timeout, especially when requesting abroad, it is easy to timeout. This is very important. If it is formal development, the maximum tolerance for remote exception requests is 5 seconds. If you cannot get the result, it will directly report an error, which has seriously affected the operation of the business.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //If it is true, false will output directly.  If we need to process the relevant strings inside, we need to set to true. If we just forward the static web page content, we can set to false and return it directly
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Whether to verify the certificate? If it is a link related to Alipay, you need to verify the digital certificate. By default, it is only the requested content, and no secure link can be established. Most of the certificates are not used. The path to deploy the certificate is required to use the certificate.
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"); //The user-Agent in the request. Generally, PHP requests its own legal links, and the requirements for this disguise are not high. If you need to disguise, it is more convenient to use python.
curl_setopt($ch,CURLOPT_HEADER, 1);// Setting the response header to get curl does not get the response header by default, which is different from python

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

For more detailed parameters, you need to check the parameter manual. The above are some parameter configurations used in development.

$response = curl_exec($ch); //After the parameter related configuration is completed, issue a request so that the result of the page request is obtained
 curl_close($ch); //After the general request completes this page, we need to close and release the resource to reduce the memory-related consumption
  • 1
  • 2

If the business is smooth, we don't need to know the error request code, but if it is a requestabnormal, we need to logically separate the error code. This is the key to solving the problem. Basically, how to deal with errors (more than 90% of the logic is correct, but encountering 10% of the wrong requests will paralyze our system, so the focus is on monitoring requests. In development, we do not pay attention to monitoring this error and suffer a great loss).

      if ($response === false) { //Request exception needs to enter logging
            $errorCode = curl_errno($ch); //The error code we got is the number.
            echo "CURL Error Code: ". $errorCode. "\n"; 
        }Related error codes1. `CURLE_OK` (0):It means that the operation is successful and there are no errors.2. `CURLE_UNSUPPORTED_PROTOCOL` (1):Unsupported protocol.3. `CURLE_FAILED_INIT` (2):Initialization failed.4. `CURLE_URL_MALFORMAT` (3): URLError in format.5. `CURLE_COULDNT_RESOLVE_PROXY` (5):The proxy server cannot be resolved.6. `CURLE_COULDNT_RESOLVE_HOST` (6):The host cannot be resolved.7. `CURLE_COULDNT_CONNECT` (7):Unable to connect to the host or proxy.8. `CURLE_OPERATION_TIMEDOUT` (28):Operation timed out.9. `CURLE_SSL_CONNECT_ERROR` (35): SSLConnection error.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Expand usage requirements: (Get more detailed response settings)

The curl_getinfo function can obtain the following common parameter information:CURLINFO_EFFECTIVE_URL: Get the final actual requestURLCURLINFO_HTTP_CODE: GetHTTPResponse status code.CURLINFO_TOTAL_TIME: The total time it takes to obtain the entire request (includingDNSparsing, connection establishment, data transmission, etc.), in seconds.CURLINFO_NAMELOOKUP_TIME: GetDNSThe time taken to parse, in seconds.CURLINFO_CONNECT_TIME: Get the time taken to establish a connection, in seconds.CURLINFO_PRETRANSFER_TIME: Gets the time taken from the start to the preparation of transmission, in seconds.CURLINFO_SIZE_UPLOAD: Get the uploaded data size in bytes.CURLINFO_SIZE_DOWNLOAD: Get the downloaded data size in bytes.CURLINFO_SPEED_DOWNLOAD: Get download speed in bytes/Seconds are units.CURLINFO_SPEED_UPLOAD: Get upload speed in bytes/Seconds in units
 
   Example usage:
  $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //HTTP code is used relatively frequently. Status code is a common judgment
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Basically, the daily use of remote network pages is used, and common protocols basically use these settings. It can complete the URL response monitoring, remote interface data acquisition, DNS response speed acquisition, and determine whether the host still exists (the key to determining the valid network domain name).