Home > Backend Development > PHP Tutorial > cURL function library error code description PHP curl_errno function_PHP tutorial

cURL function library error code description PHP curl_errno function_PHP tutorial

WBOY
Release: 2016-07-13 10:57:48
Original
1279 people have browsed it

This article will introduce to you the PHP curl_errno function that explains the error code of the cURL function library. Friends who need to know more can refer to it.

Background overview:
The game interface uses the PHP cURL extension to perform request operations. However, the requested server often fails to respond or times out for no apparent reason. In short, the response data is not received after the request. At this time, you cannot say that there is a problem with the other party's API interface, or that the server is faulty. In short, there are many possible problems. It cannot be generalized.

1. Give a commonly used PHP cURL code:

The code is as follows Copy code
 代码如下 复制代码

function sendRequestGame($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;
}

function sendRequestGame($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 );
 代码如下 复制代码

function sendRequestGame($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;
}

/**
 * 打日志。
 * @param string $log 日志内容。
 * @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 );
}

return $retData; }
People who often use the PHP cURL function must be familiar with it. At the same time, I also believe that most PHPers write code this way. At least that's the core part. There's no way to determine why the request failed and what happened. Admittedly, a serious security incident caused me to re-review this cURL library. How can I ensure that my requests are stable and reliable? When a failure occurs, I want to know what caused it. Know and feedback to people as soon as possible, and conduct timely communication, coordination and repair. Now in order to ensure the stability and reliability of each request, we must add a logging function. That is, the relevant parameter status and error code requested when the request fails are recorded in the log. Convenient, we will check after failure. Look at the code:
The code is as follows Copy code
function sendRequestGame($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 ​*/ 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 ); }

Now when the sendRequestGame function is called, the information of each request will be sent to json_encode and then saved to the log file curl_log.txt. In this way, we can clearly know what happened with each request.

After improvement, two functions have been added:

$errno = curl_errno( $ch );
$info = curl_getinfo( $ch ); These two functions are very critical. The first curl_errno returns the error code of the current request. 0 means there is no error and it is an OK normal request. An error occurred with a non-zero code request. However, most errors occur when the request does not correctly reach the server specified by the URL. For example: host cannot be reached, URL error, 404. Of course, the existence of internal server errors such as 500 cannot be ruled out.

The second one 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] => 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] =>

Array ( [url] => http://www.bKjia.c0m/ [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 everyone can understand 7788 literally. If you don’t understand, go read the official PHP manual yourself.

Below, I will attach the curl error code, which is the numerical description returned by the curl_errno function:
CURLE_UNSUPPORTED_PROTOCOL (1) – The URL you passed to libcurl uses a protocol that libcurl does not support. It may be that a compile-time option you are not using is causing this (perhaps the protocol string is spelled incorrectly, or the protocol libcurl code is not specified).
CURLE_FAILED_INIT (2) – Very early initialization code failed. Could be an internal error or problem.
CURLE_URL_MALFORMAT (3) – URL format is incorrect.
CURLE_COULDNT_RESOLVE_PROXY (5) – Unable to resolve proxy server. The specified proxy server host cannot be resolved.
CURLE_COULDNT_RESOLVE_HOST (6) – Unable to resolve host. The specified remote host cannot be resolved.
CURLE_COULDNT_CONNECT (7) – Unable to connect() to host or proxy server.
CURLE_FTP_WEIRD_SERVER_REPLY (8) – libcurl needs to receive a specific reply after connecting to an FTP server. This error code indicates that an unusual 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 resource specified in the URL. For FTP, this happens if you try to change to a remote directory.
CURLE_FTP_WEIRD_PASS_REPLY (11) – libcurl needs to receive a correct reply after sending the FTP password to the server. This error code indicates that an unexpected code was returned.
CURLE_FTP_WEIRD_PASV_REPLY (13) – libcurl was unable to receive useful results from the server side in response to PASV or EPSV commands. There is a problem with the server.
CURLE_FTP_WEIRD_227_FORMAT (14) – The FTP server returns line 227 in response to the PASV command. This code is returned if libcurl cannot parse the line.
CURLE_FTP_CANT_GET_HOST (15) – An internal error occurred while locating a host for a 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) – The file transfer size was smaller or larger than expected. This error occurs when the server first reports an expected transfer size and then transfers data that does not match the previously specified size.
CURLE_FTP_COULDNT_RETR_FILE (19) – The ‘RETR’ command received an abnormal reply, or the completed transfer size was zero bytes.
CURLE_QUOTE_ERROR (21) – When sending custom “QUOTE” commands to a remote server, one of the commands returned an error code of 400 or a larger number (for FTP), or otherwise indicated that the command could not complete 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 >= 400. (This error code was previously 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.
CURLE_UPLOAD_FAILED (25) – Unable to start upload. For FTP, the server usually refuses to execute the STOR command. The error buffer usually provides the server's explanation of the problem. (This error code was previously known as CURLE_FTP_COULDNT_STOR_FILE.)
CURLE_READ_ERROR (26) – A problem was encountered 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 timed out. The timeout specified based on the situation has been reached. Please note: As of Urchin 6.6.0.2, the timeout can be changed yourself. To specify a remote log download timeout, open the urchin.conf file and uncomment the following line:
#DownloadTimeout: 30
CURLE_FTP_PORT_FAILED (30) – The FTP PORT command returned an error. This problem is most likely to occur when proper address usage is not specified for libcurl. See CURLOPT_FTPPORT.
CURLE_FTP_COULDNT_USE_REST (31) – FTP REST command returned an error. If the server is healthy, this should not happen.
CURLE_RANGE_ERROR (33) – The server does not support or accept the range request.
CURLE_HTTP_POST_ERROR (34) – This problem is rare and is caused by internal chaos.
CURLE_SSL_CONNECT_ERROR (35) – This error can occur when using SSL/TLS simultaneously. You can access the error buffer to see the information, which describes the issue in more detail. Certificates (file format, path, permissions), passwords, and other factors may be causing the issue.
CURLE_FTP_BAD_DOWNLOAD_RESUME (36) – Attempt to resume an FTP connection that exceeded the file size limit.
CURLE_FILE_COULDNT_READ_FILE (37) – Unable to open file in FILE:// path. The reason is most likely that the file path does not recognize the existing file. It is recommended that you check the access permissions of the file.
CURLE_LDAP_CANNOT_BIND (38) – LDAP cannot bind. LDAP bind operation failed.
CURLE_LDAP_SEARCH_FAILED (39) – LDAP search failed.
CURLE_FUNCTION_NOT_FOUND (41) – Function not found. Required zlib function not found.
CURLE_ABORTED_BY_CALLBACK (42) – Aborted by callback. The callback returned "abort" to libcurl.
CURLE_BAD_FUNCTION_ARGUMENT (43) – Internal error. A function was called with incorrect parameters.
CURLE_INTERFACE_FAILED (45) – Interface error. The specified external interface cannot be used. Use CURLOPT_INTERFACE to set which interface to use to handle the source IP address of external connections. (This error code was previously known as CURLE_HTTP_PORT_FAILED.)
CURLE_TOO_MANY_REDIRECTS (47) – Too many redirects. While redirecting, libcurl reached the page click limit. Please use CURLOPT_MAXREDIRS to set a limit.
CURLE_UNKNOWN_TELNET_OPTION (48) – Options set with CURLOPT_TELNETOPTIONS are not recognized. See related documentation.
CURLE_TELNET_OPTION_SYNTAX (49) – The telnet option string is not in the correct format.
CURLE_PEER_FAILED_VERIFICATION (51) – The remote server's SSL certificate or SSH md5 fingerprint is incorrect.
CURLE_GOT_NOTHING (52) – The server did not return any data, in which case an error occurred.
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) – The specified key cannot be used
CURLE_SSL_CACERT (60) – Unable to verify peer certificate using known CA certificate
CURLE_BAD_CONTENT_ENCODING (61) – Transfer encoding not recognized
CURLE_LDAP_INVALID_URL (62) – Invalid LDAP URL
CURLE_FILESIZE_EXCEEDED (63) – Maximum file size exceeded
CURLE_USE_SSL_FAILED (64) – The requested FTP SSL level failed
CURLE_SEND_FAIL_REWIND (65) – When performing a send operation, curl had to roll around the data for retransmission, but the rollback operation failed
CURLE_SSL_ENGINE_INITFAILED (66) – SSL engine initialization failed
CURLE_LOGIN_DENIED (67) – The remote server refused curl login (new in 7.13.1)
CURLE_TFTP_NOTFOUND (68) – File not found on TFTP server
CURLE_TFTP_PERM (69) – Encountering permission issues on the TFTP server
CURLE_REMOTE_DISK_FULL (70) – Not enough disk space on the server
CURLE_TFTP_ILLEGAL (71) – Illegal TFTP operation
CURLE_TFTP_UNKNOWNID (72) – TFTP transport ID unknown
CURLE_REMOTE_FILE_EXISTS (73) – File already exists and cannot be overwritten
CURLE_TFTP_NOSUCHUSER (74) – A functioning TFTP server will not return this error
CURLE_CONV_FAILED (75) – Character conversion failed
CURLE_CONV_REQD (76) – The caller must register a conversion callback
CURLE_SSL_CACERT_BADFILE (77) – Problem reading SSL CA certificate (possible path error or access rights issue)
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632079.htmlTechArticleThis article will introduce to you the PHP curl_errno function that explains the cURL function library error code. Friends who need to know more about it are welcome. Enter reference. Background overview: The game interface is made using the PHP cURL extension...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template