PHP crawls data through cURL (2): CURLINFO_HTTP_CODE returns 0

1. Project Description

Xiaogetong is a platform that provides online video services such as live broadcast, recorded broadcast, and short video. Recently, common problems in docking with its API include:

  1. Authorization issue: Before using Gooselink API, authorization verification is required to ensure the legality of accessing the API. If the authorization information is wrong or invalid, the API request will fail.

  2. Parameter error: When calling the Gooselink API, you need to pass in the corresponding parameters. If the parameters are wrong or missing required parameters, the API request will fail.

  3. Network connection issues: API requests may fail due to network issues.

  4. Frequency limit problem: Goose Express has a certain limit on the frequency of API requests. If you call the API frequently, the API call may be restricted by Goose Express or the API access right may be closed.

  5. API returns exception: The data format, content or status returned by the Gooselink API request may be abnormal, and the abnormal situation needs to be handled.
    insert image description here

2. curl_getinfo returns an exception

1. Gooselink SDK

    public static function curlGet($url, $queryparas = array(), $timeout = 5, $header = array(), $proxy = array())
    {
    
    
        if (!empty($queryparas)) {
    
    
            if (is_array($queryparas)) {
    
    
                $postData = http_build_query($queryparas);
                $url .= strpos($url, '?') ? '' : '?';
                $url .= $postData;
            } else if (is_string($queryparas)) {
    
    
                $url .= strpos($url, '?') ? '' : '?';
                $url .= $queryparas;
            }
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($header) && is_array($header)) {
    
    
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        if (!empty($proxy)) {
    
    
            curl_setopt($ch, CURLOPT_PROXYAUTH, 1);
            curl_setopt($ch, CURLOPT_PROXY, $proxy['ip']);
            curl_setopt($ch, CURLOPT_PROXYPORT, $proxy['port']);
            curl_setopt($ch, CURLOPT_PROXYTYPE, 0);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        $output = curl_exec($ch);
        if (is_array(json_decode($output, true))) {
    
    
            $output = json_decode($output, true);
        }

        $result['status_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $result['body'] = $output;

        curl_close($ch);
        return $result;
    }

 curl_getinfo($ch, CURLINFO_HTTP_CODE);在使用过程中偶尔出现,会导致token掉包,无法更新用户信息。

2. What are the reasons why CURLINFO_HTTP_CODE is 0?

When using the cURL function in PHP to send an HTTP request, the CURLINFO_HTTP_CODEreturned 0 may have the following reasons:

  1. cURL cannot connect to the server

If the server cannot be reached, it may be due to a network failure or server unavailability. In this case, the http code returned by cURL is 0.

  1. Request timed out

cURL may fail to complete a request because the request times out, which may be due to a long server response time or a network communication error. In this case, the http code returned by cURL is 0.

  1. The server returned an HTTP response code of 0

If your server returns an unusual response status code (such as 000), cURL's response status code can be set to 0.

  1. cURL options setting problem

If you are using cURL without properly setting the required options, it may not handle links correctly. You should make sure that the URL is correct, that the HTTP request is set up correctly, and that you do not omit any required options such as CURLOPT_RETURNTRANSFER, CURLOPT_HEADER, CURLOPT_USERAGENT, etc.

The above are some reasons that may cause curl_getinfo CURLINFO_HTTP_CODEto 0, which needs to be checked according to the specific situation.

3. Return the solution of CURLINFO_HTTP_CODE=0

Sympathy : Portal
insert image description here
host no found, can't find the host? In the project, it is mainly the token verified with Goose Cloud curl. This situation will occur when people use it intensively. It is normal to change servers. The biggest reason should be the problem of the server used.

4. Is the request timeout related to server configuration and CPU usage?

There is a certain relationship between request timeout and server configuration and CPU usage.

When a server's CPU usage is high, the server may process requests more slowly, causing requests to time out. This is because the server needs to use CPU processing logic when processing requests, and if the server's CPU usage is too high, it may slow down the processing of requests, resulting in request timeouts.

In addition, improper server configuration may also have an impact on request timeouts. For example, when the server is under-configured, the server may not be able to handle a large number of concurrent requests, causing requests to time out. Therefore, in order to avoid the problem of request timeout, it is necessary to adjust the server configuration according to the server load and the amount of requests to be processed to ensure that the server can run stably and process requests efficiently.

In short, there is a relationship between request timeout and server configuration and CPU usage. It is necessary to adjust the server configuration reasonably to ensure that the server can meet the processing requirements of the request and improve the performance and stability of the server.

insert image description here
insert image description here
insert image description here

5 Conclusion

  • Judging from the logs returned by Little Goose Cloud, cURL always works;
  • When the official environment server cannot be connected normally, the acquisition of the test server is normal;
  • Looking at the running logs and different time nodes, it should be that the speed of the server processing requests slows down due to traffic peaks, and the requests time out;

3. Alibaba Cloud SMS is sent in a centralized manner after delay

1. Analysis of the reasons for sending centralized

  1. SMS sending frequency limit

Alibaba Cloud SMS service has a sending frequency limit. If a user sends a large number of SMS messages to the same target phone number in a short period of time, Alibaba Cloud SMS service may cache these SMS messages and wait for a certain period of time before sending them. This will cause the short message sending delay and centralized sending.

  1. server load

If the server you are using is heavily loaded, it may cause delays in sending SMS messages. Since SMS sending usually requires network communication with Alibaba Cloud SMS service, if the load on the server is too high, network communication will also slow down, thus affecting the speed of SMS sending.

  1. Alibaba Cloud SMS service failure

If the Alibaba Cloud SMS service fails or is under maintenance, it may cause a delay in sending SMS messages. At the same time, after the service is restored, there may be some centralized sending of SMS messages.

  1. SMS sending service call problem

If you use the SMS sending SDK or interface to call the Alibaba Cloud SMS service, there are calling problems, such as request timeout or network fluctuations, which may also cause delays in sending SMS messages.

2. Conclusion

According to the API docking and CPU operation log of Xiaogetong, the server cannot bear such a large traffic.

Let's upgrade the server!


@leak time sometimes

Guess you like

Origin blog.csdn.net/weixin_41290949/article/details/130980664