Error Codes of the cURL function library: PHP curl_errno Function

Source: Internet
Author: User
Tags ftp connection ssl connection

This article introduces the PHP curl_errno function described by the cURL function library error code.

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: Copy code

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: Copy code

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: Copy code

Array
(
[Url] => http://www.bKjia. c0m/
[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.