Background Overview:
The game interface is a request operation using the PHP curl extension. However, the requested server is often unresponsive or timed out for no reason. In short, the response is not received after the request data. This time can not say that the other API interface problems, or the server is faulty. In short, the problem that may arise is very much. Can't generalize.
First, give a piece of commonly used PHP Curl code:
functionSendrequestgame ($url){ $header=Array(' Expect: '); $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;}
People who often use PHP curl functions will never be strangers.
At the same time, I also believe that most phper also write code like this. At least that's the core part, and there's no way of judging the failure of this request and what happened.
Admittedly, I had to re-examine the curl library through a serious security incident to ensure that my request was stable and reliable. When there is a failure, I want to know what caused the problem. The first time to know and feedback to the person, the timely communication and coordination and repair.
Now we must join the log function in order to guarantee the stable reliability of each request. That is, the state of the requested parameter and the error code are recorded in the log when the failure occurs. Convenient, after we failed to check.
Look at the code:
functionSendrequestgame ($url){ $header=Array(' Expect: '); $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 log content. * @return void*/functionPutlog ($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);}
Now when the Sendrequestgame function is called, it will be saved to the log file Curl_log.txt after each requested message to Json_encode. In this way, we can clearly know what happens to each request.
After the improvement, two functions were added:
$errno = Curl_errno ($ch);
$info = Curl_getinfo ($ch), these two functions are very critical. The first Curl_errno is the error code that returns the current request, 0 means no error and is an OK request. An error has occurred with a non-0 code request. However, most errors occur when the request does not correctly reach the server specified by the URL. Such as: The host cannot hit, url error, 404. Of course, there are 500 of such internal server errors that are not ruled out.
The second is that the function is very important, and the Curl_getinfo function obtains information about the current request:
Array([url]= = http://www.111cn.net/[Content_Type] = text/html; Charset=utf-8[Http_code]= 200[Header_size]= 321[Request_size]= 53[FILETIME]+ =-1[Ssl_verify_result]= 0[Redirect_count]= 0[Total_time]= 2.075[Namelookup_time]= 0[Connect_time]= 0.031[Pretransfer_time]= 0.031[Size_upload]= 0[Size_download]= 79042[Speed_download]= 38092[Speed_upload]= 0[Download_content_length]+ =-1[Upload_content_length]= 0[Starttransfer_time]= 1.388[Redirect_time]= 0[Certinfo]=Array() [Redirect_url]=
I believe that people can literally read 7788. If you don't understand, go to the official PHP manual.
Below, I enclose the Curl error code, which is the numeric description returned by the Curl_errno function:
Curle_unsupported_protocol (1) – The URL you send to Libcurl uses a protocol that is not supported by this libcurl. This may be caused by a compile-time option that you did not use (either the protocol string was spelled incorrectly or the protocol Libcurl code was not specified).
Curle_failed_init (2) – Very early initialization code failed. may be an internal error or issue.
Curle_url_malformat (3) – The URL format is incorrect.
Curle_couldnt_resolve_proxy (5) – the proxy server could not be resolved. The specified proxy server host could not be resolved.
Curle_couldnt_resolve_host (6) – Unable to resolve host. The specified remote host could not be resolved.
Curle_couldnt_connect (7) – Unable to connect to the host or proxy server via 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 reply was 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 to a remote directory.
Curle_ftp_weird_pass_reply (11) – After the FTP password is sent to the server, Libcurl needs to receive the correct response. This error code indicates that the code returned is unexpected.
Curle_ftp_weird_pasv_reply (13) –libcurl cannot receive useful results from the server as a response to a PASV or EPSV command. There is a problem with the server.
Curle_ftp_weird_227_format (14) The –FTP server returns 227 rows as a response to the PASV command. If Libcurl cannot parse this line, it will return this code.
Curle_ftp_cant_get_host (15) – An internal error occurred while locating the host for the new connection.
Curle_ftp_couldnt_set_type (17) – An error occurred while trying to set the transfer mode to binary or ASCII.
Curle_partial_file (18) – File transfer size is less than or greater than expected. This error occurs when the server first reports an expected transfer size, and then the transmitted data does not match the previously specified size.
Curle_ftp_couldnt_retr_file (19) – The ' RETR ' command received an unhealthy response, or the completed transfer size is 0 bytes.
Curle_quote_error (21) – When a custom "QUOTE" command is sent to a remote server, one of the commands returns a number with an error code of 400 or greater (for FTP), or otherwise indicates that the command could not be completed successfully.
Curle_http_returned_error (22) – This code is returned if Curlopt_failonerror is set to TRUE and the HTTP server returns an error code of >= 400. (This error code was formerly known as Curle_http_not_found.) )
Curle_write_error (23) – An error occurred while writing the received data to the local file, or an error was returned to Libcurl by the write callback (write callback).
Curle_upload_failed (25) – Unable to start uploading. For FTP, the server typically refuses to execute the STOR command. The error buffer typically provides a description of the problem to the server. (This error code was formerly known as Curle_ftp_couldnt_stor_file.) )
Curle_read_error (26) – a problem was encountered while reading a local file, or an error was returned by the read callback callback.
Curle_out_of_memory (27) – Memory allocation request failed. This error is more severe and if this error occurs, a very serious problem has occurred.
Curle_operation_timedout (28) – Operation timed out. The time-out specified according to the appropriate conditions has been reached. Please note: The time-out can be changed from Urchin 6.6.0.2 onwards. To specify a remote log download timeout, open the urchin.conf file and uncomment the following line:
#DownloadTimeout: 30
Curle_ftp_port_failed (30) –ftp PORT command returned an error. This issue is most likely to occur when the appropriate address is not specified for Libcurl. See Curlopt_ftpport.
Curle_ftp_couldnt_use_rest (31) The –FTP REST command returned an error. This should not happen if the server is healthy.
Curle_range_error (33) – The server does not support or accept range requests.
Curle_http_post_error (34) – This problem is relatively rare and is mainly caused by internal confusion.
Curle_ssl_connect_error (35) – This error may occur when SSL/TLS is used together. You can access the error buffer to see the appropriate information, which is described in more detail in this issue. This issue may be caused by a certificate (file format, path, license), password, and other factors.
Curle_ftp_bad_download_resume (36) – try to recover an FTP connection that exceeds the file size limit.
Curle_file_couldnt_read_file (37) – Unable to open file under file://path. The most likely reason is that the file path does not recognize the existing file. It is recommended that you check the file access permissions.
Curle_ldap_cannot_bind (38) –ldap cannot bind. The LDAP bind operation failed.
Curle_ldap_search_failed (39) –ldap search is not possible.
Curle_function_not_found (41) – Function not found. The necessary zlib function was not found.
Curle_aborted_by_callback (42) – aborted by callback. The callback returned "abort" to Libcurl.
Curle_bad_function_argument (43) – Internal error. The function was called with an incorrect argument.
Curle_interface_failed (45) – Interface error. The specified external interface is not available. Please set which interface to use to handle the source IP address of the external connection via curlopt_interface. (This error code was formerly known as Curle_http_port_failed.) )
Curle_too_many_redirects (47) – Too many redirects. When you redirect, the libcurl reaches the maximum page hit limit. Please use Curlopt_maxredirs to set the upper limit.
Curle_unknown_telnet_option (48) – the option set with Curlopt_telnetoptions is not recognized. Please refer to the relevant documentation.
Curle_telnet_option_syntax (49) The –telnet option string is not formatted correctly.
Curle_peer_failed_verification (51) – The remote server's SSL certificate or SSH MD5 fingerprint is incorrect.
Curle_got_nothing (52) – The server does not return any data, and in the appropriate case, no data is returned in the event of an error.
Curle_ssl_engine_notfound (53) – The specified encryption engine could not be found.
Curle_ssl_engine_setfailed (54) – The selected SSL encryption engine cannot be set as the default option.
Curle_send_error (55) – Unable to send network data.
Curle_recv_error (56) – Failed to receive network data.
Curle_ssl_certproblem (58) – there is a problem with the local client certificate
Curle_ssl_cipher (59) – Cannot use the specified key
Curle_ssl_cacert (60) – Unable to validate peer certificate with known CA certificate
Curle_bad_content_encoding (61) – Transmission encoding not recognized
Curle_ldap_invalid_url (62) –ldap URL is invalid
Curle_filesize_exceeded (63) – exceeds the maximum file size
Curle_use_ssl_failed (64) – The requested FTP SSL level failed
Curle_send_fail_rewind (65) – When a send operation occurs, curl must rotate the data for retransmission, but the rotation operation is unsuccessful
Curle_ssl_engine_initfailed (66) –ssl engine initialization failed
Curle_login_denied (67) – Remote server rejects Curl login (7.13.1 new feature)
Curle_tftp_notfound (68) – File not found on the TFTP server
Curle_tftp_perm (69) – A permissions issue was encountered on the TFTP server
Curle_remote_disk_full (70) – Insufficient server disk space
Curle_tftp_illegal (71) –TFTP operation illegal
Curle_tftp_unknownid (72) –tftp Transfer ID Unknown
Curle_remote_file_exists (73) – File already exists and cannot be overwritten
Curle_tftp_nosuchuser (74) – A functioning TFTP server does not return this error
Curle_conv_failed (75) – Character conversion failed
CURLE_CONV_REQD (76) – Caller must register conversion callback
Curle_ssl_cacert_badfile (77) – a problem was encountered while reading the SSL CA certificate (possibly a path error or access problem)
Curle_remote_file_not_found (78) – the resource referenced 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 SSL connection
The PHP Curl_errno function for the error code description of the Curl function library