Background:
The game interface uses PHP cURL extension for request operations. However, the requested server often does not respond or times out for no reason. In short, the data cannot be returned after the request. At this time, it cannot be said that the other party's API interface is faulty, or the server is faulty. In short, there may be many problems. Cannot be generalized.
1. A common PHP cURL code is provided:
The code is as follows: |
|
Function sendRequestGame ($ url) { $ Header = array ('CT :'); $ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header ); Curl_setopt ($ ch, CURLOPT_TIMEOUT, 10 ); Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10 ); $ RetData = curl_exec ($ ch ); Curl_close ($ ch ); Return $ retData; } |
PHP cURL functions are often used.
At the same time, I also believe that most PHP users write code like this. At least this is the core part. It does not determine the failure of the request.
It is true that, due to a serious security accident, I have to review this cURL Library. How can I ensure that my requests are stable and reliable. When a failure occurs, I want to know the cause. Inform people immediately for timely communication, coordination, and repair.
Now, to ensure the stability and reliability of each request, the log function must be added. The request parameters and error codes are recorded in the log when the request fails. This is convenient. We will check the failure.
Check the code:
The code is as follows: |
|
Function sendRequestGame ($ url) { $ Header = array ('CT :'); $ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header ); Curl_setopt ($ ch, CURLOPT_TIMEOUT, 2 ); Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 3 ); $ Return = curl_exec ($ ch ); $ Errno = curl_errno ($ ch ); $ Info = curl_getinfo ($ ch ); $ Info ['errno'] = $ errno; Curl_close ($ ch ); $ Log = json_encode ($ info ); PutLog ($ log ); Return $ return; } /** * Log. * @ Param string $ log content. * @ Return void */ Function putLog ($ log) { $ Log. = "nn "; $ LogDir = dirname (_ FILE __); $ LogPath = $ logDir. "/curl_log.txt "; If (! File_exists ($ logPath )) { $ Handle = fopen ($ logPath, 'w '); Fclose ($ handle ); } File_put_contents ($ logPath, $ log, FILE_APPEND ); } |
When the sendRequestGame function is called, the information of each request is sent to json_encode and saved to the log file curl_log.txt. In this way, we can clearly know what happened to each request.
After improvement, two functions are added:
$ Errno = curl_errno ($ ch );
$ Info = curl_getinfo ($ ch); these two functions are critical. The first curl_errno is the error code returned for the current request. 0 indicates no error and is a normal OK request. A non-zero code request error occurs. However, when most errors occur, the requests do not arrive at the server specified by the URL correctly. For example, the host cannot be accessed, the website address is incorrect, and the website address is 404. Of course, the existence of 500 Internal Server errors is not ruled out.
The second is that the function is very important. The curl_getinfo function will obtain the relevant information of the current request:
The code is as follows: |
|
Array ( [Url] => http://www.111cn.net/ [Content_type] => text/html; charset = UTF-8 [Http_code] = & gt; 200 [Header_size] = & gt; 321 [Request_size] => 53 [Filetime] =>-1 [Ssl_verify_result] => 0 [Redirect_count] => 0 [Total_time] = & gt; 2.075 [Namelookup_time] => 0 [Connect_time] = & gt; 0.031 [Pretransfer_time] = & gt; 0.031 [Size_upload] => 0 [Size_download] = & gt; 79042 [Speed_download] = & gt; 38092 [Speed_upload] => 0 [Download_content_length] =>-1 [Upload_content_length] => 0 [Starttransfer_time] = & gt; 1.388 [Redirect_time] => 0 [Certinfo] => Array ( ) [Redirect_url] => |
I believe that everyone can understand the meaning of the word 7788. If you don't understand it, go to the official PHP Manual.
Below, I will attach the curl error code, that is, the number description returned by the curl_errno function:
CURLE_UNSUPPORTED_PROTOCOL (1)-the URL you send to libcurl uses a protocol not supported by this libcurl. The possible cause is that you do not use the compile-time option (it may be that the protocol string is misspelled or the protocol libcurl code is not specified ).
CURLE_FAILED_INIT (2)-very early initialization code failed. It may be an internal error or problem.
CURLE_URL_MALFORMAT (3)-The URL format is incorrect.
CURLE_COULDNT_RESOLVE_PROXY (5)-the proxy server cannot be resolved. The specified proxy server host cannot be parsed.
CURLE_COULDNT_RESOLVE_HOST (6)-The host cannot be resolved. The specified remote host cannot be parsed.
CURLE_COULDNT_CONNECT (7)-you cannot connect to the host or proxy server through connect.
CURLE_FTP_WEIRD_SERVER_REPLY (8)-After connecting to the FTP server, libcurl needs to receive a specific response. This error code indicates that an abnormal or incorrect response is received. The specified remote server may not be the correct FTP server.
CURLE_REMOTE_ACCESS_DENIED (9)-we cannot access the resources specified in the URL. For FTP, this happens if you try to change it to a remote directory.
CURLE_FTP_WEIRD_PASS_REPLY (11)-After the FTP password is sent to the server, libcurl needs to receive a correct response. This error code indicates that unexpected code is returned.
CURLE_FTP_WEIRD_PASV_REPLY (13)-libcurl cannot receive useful results from the server as a response to the PASV or EPSV command. The server is faulty.
CURLE_FTP_WEIRD_227_FORMAT (14)-The FTP server returns row 227 as a response to the PASV command. If libcurl cannot parse this line, this code is returned.
CURLE_FTP_CANT_GET_HOST (15)-an internal error occurs when you look for a host for a new connection.
CURLE_FTP_COULDNT_SET_TYPE (17)-An error occurred while trying to set the transmission mode to binary or ascii.
CURLE_PARTIAL_FILE (18)-The file transfer size is smaller than or greater than expected. This error occurs when the server first reports an expected transfer size and the transmitted data does not match the previously specified size.
The CURLE_FTP_COULDNT_RETR_FILE (19)-'retr' command receives an abnormal response, or the size of the completed transfer is zero bytes.
CURLE_QUOTE_ERROR (21)-When a custom "QUOTE" command is sent to a remote server, the error code returned by one of the commands is 400 or a larger number (for FTP ), otherwise, the command cannot be completed successfully.
CURLE_HTTP_RETURNED_ERROR (22)-If CURLOPT_FAILONERROR is set to TRUE and the HTTP server returns an error code greater than or equal to 400, this code is returned. (This error code is also called CURLE_HTTP_NOT_FOUND .)
CURLE_WRITE_ERROR (23)-an error occurs when writing data received to a local file, or an error is returned to libcurl by write callback.
CURLE_UPLOAD_FAILED (25)-upload cannot begin. For FTP, the server usually rejects the execution of the STOR command. The error buffer usually provides server instructions for this problem. (This error code is also called CURLE_FTP_COULDNT_STOR_FILE .)
CURLE_READ_ERROR (26)-An error occurred while reading the local file, or an error was returned by the read callback.
CURLE_OUT_OF_MEMORY (27)-Memory Allocation request failed. This error is serious. If this error occurs, it indicates that a very serious problem has occurred.
CURLE_OPERATION_TIMEDOUT (28)-Operation timeout. The time-out period has been reached according to the corresponding situation. Note: The timeout value can be changed on your own since Urchin 6.6.0.2. To specify that the remote log download times out, open the urchin. conf file and uncomment the following lines:
# DownloadTimeout: 30
CURLE_FTP_PORT_FAILED (30)-The ftp port command returns an error. This problem is most likely to occur when no appropriate address is specified for libcurl. See CURLOPT_FTPPORT.
CURLE_FTP_COULDNT_USE_REST (31)-The ftp rest command returns an error. This should not happen if the server is normal.
CURLE_RANGE_ERROR (33)-the server does not support or accept range requests.
CURLE_HTTP_POST_ERROR (34)-this problem is rare and is mainly caused by internal chaos.
CURLE_SSL_CONNECT_ERROR (35)-this error may occur when both SSL and TLS are used. You can access the error buffer to view the relevant information, which will be described in more detail. This problem may be caused by the certificate (file format, path, license), password, and other factors.
CURLE_FTP_BAD_DOWNLOAD_RESUME (36)-Attempts to restore an FTP connection that exceeds the file size limit.
CURLE_FILE_COULDNT_READ_FILE (37)-files in the FILE: // path cannot be opened. The possible cause is that the file path cannot identify the existing file. We recommend that you check the file access permissions.
CURLE_LDAP_CANNOT_BIND (38)-LDAP cannot be bound. LDAP binding failed.
CURLE_LDAP_SEARCH_FAILED (39)-LDAP Search fails.
CURLE_FUNCTION_NOT_FOUND (41)-The function cannot be found. The necessary zlib function cannot be found.
CURLE_ABORTED_BY_CALLBACK (42)-aborted by the callback. The callback returns "abort" to libcurl ".
CURLE_BAD_FUNCTION_ARGUMENT (43)-Internal error. An incorrect parameter is used to call a function.
CURLE_INTERFACE_FAILED (45)-interface error. The specified external interface cannot be used. Use CURLOPT_INTERFACE to set the interface to be used to process the source IP address of the external connection. (This error code is also called CURLE_HTTP_PORT_FAILED .)
CURLE_TOO_MANY_REDIRECTS (47)-Excessive redirection. During redirection, the libcurl reaches the webpage click limit. Use CURLOPT_MAXREDIRS to set the upper limit.
CURLE_UNKNOWN_TELNET_OPTION (48)-the options set with CURLOPT_TELNETOPTIONS cannot be identified. See related documents.
CURLE_TELNET_OPTION_SYNTAX (49)-the format of the telnet Option string is incorrect.
CURLE_PEER_FAILED_VERIFICATION (51)-The SSL certificate or SSH md5 fingerprint of the remote server is incorrect.
CURLE_GOT_NOTHING (52)-the server does not return any data. If no data is returned, an error occurs.
CURLE_SSL_ENGINE_NOTFOUND (53)-The specified encryption engine cannot be found.
CURLE_SSL_ENGINE_SETFAILED (54)-The selected SSL encryption engine cannot be set as the default option.
CURLE_SEND_ERROR (55)-Network data cannot be sent.
CURLE_RECV_ERROR (56)-failed to receive network data.
CURLE_SSL_CERTPROBLEM (58)-local client certificate error
CURLE_SSL_CIPHER (59)-The specified key cannot be used.
CURLE_SSL_CACERT (60)-Unable to use a known CA certificate to verify the peer certificate
CURLE_BAD_CONTENT_ENCODING (61)-unrecognized transfer encoding
CURLE_LDAP_INVALID_URL (62)-invalid LDAP URL
CURLE_FILESIZE_EXCEEDED (63)-exceeds the file size limit
CURLE_USE_SSL_FAILED (64)-An error occurred while requesting the ftp ssl level.
CURLE_SEND_FAIL_REWIND (65)
CURLE_SSL_ENGINE_INITFAILED (66)-SSL engine initialization failed
CURLE_LOGIN_DENIED (67)-remote server rejects curl logon (7.13.1 new feature)
CURLE_TFTP_NOTFOUND (68)-File not found on the TFTP server
CURLE_TFTP_PERM (69)-permission issues on the TFTP server
CURLE_REMOTE_DISK_FULL (70)-insufficient server disk space
CURLE_TFTP_ILLEGAL (71)-The TFTP operation is invalid.
CURLE_TFTP_UNKNOWNID (72)-unknown TFTP transfer ID
CURLE_REMOTE_FILE_EXISTS (73)-The file already exists and cannot be overwritten.
CURLE_TFTP_NOSUCHUSER (74)-This error is not returned for normal TFTP servers.
CURLE_CONV_FAILED (75)-character conversion failed
CURLE_CONV_REQD (76)-the caller must register the conversion callback.
CURLE_SSL_CACERT_BADFILE (77)-An error occurred while reading the ssl ca certificate (possibly due to a path error or access permission error)
CURLE_REMOTE_FILE_NOT_FOUND (78)-The referenced resource in the URL does not exist.
CURLE_SSH (79)-An unrecognized error occurred in the SSH session
CURLE_SSL_SHUTDOWN_FAILED (80)-unable to terminate the SSL connection