curl下载失败返回0_如何从curl_errno返回值获取错误信息

当PHP使用curl函数库遇到问题时,curl_errno()返回的错误代码可以帮助定位问题。本文介绍了如何从curl_errno返回值获取错误信息,并提供了一个错误代码与错误信息的对照表,例如返回值6表示“没能解析远程主机”。通过对错误代码的分析,可以更好地解决curl操作中的问题。
摘要由CSDN通过智能技术生成

在php程序编写中,使用curl函数库的几率还是挺高的,如curl_init()、curl_setopt()、curl_exec()、curl_errno()等都是常用的函数,尤其是在获取网页相关信息的时候。

在使用这些函数的时候,有时会遇到一些问题,如之前遇到的IIS运行PHP出现Call to undefined function curl_init()的问题,这些问题一般都比较基础,遇到问题并解决问题,是一个程序员成长的历程,在解决问题的过程中能学会更多问题之外的知识。

今天在使用curl_errno()时,新的问题又出现了。如下语句:

if(!curl_errno($url)){... ...}

程序运行时,{... ...}没有执行,说明if(!curl_errno($url))条件不为真。不过是仅仅知道了条件不为真这个结果,而不知道为什么不为真,哪里出的问题?所以,这个问题需要进一步诊断,那就是从curl_errno($url)的返回值获得错误的信息。

如何从curl_errno返回值获取错误信息

curl_errno($url)会返回一个数值,这个数值可能是0,也可能是其他。当这个数值为0时,则说明没有问题,相当于返回false,而用在条件语句里if(!curl_errno($url)){... ...},加了!后就变为true。所以当返回数值是0时就执行if后面的主体语句,返回其他数值时,不执行if后面的主体语句。

因此,我们需要一个错误号和错误信息的对照表即可,根据错误号找出对应的错误信息。

下面附上错误号及错误信息的对照资料

CURLE_OK (0)

All fine. Proceed as usual.

CURLE_UNSUPPORTED_PROTOCOL (1)

The URL you passed to libcurl used a protocol that this libcurl does not support. The support might be a compile-time option that you didn't use, it can be a misspelled protocol string or just a protocol libcurl has no code for.

CURLE_FAILED_INIT (2)

Very early initialization code failed. This is likely to be an internal error or problem, or a resource problem where something fundamental couldn't get done at init time.

CURLE_URL_MALFORMAT (3)

The URL was not properly formatted.

CURLE_NOT_BUILT_IN (4)

A requested feature, protocol or option was not found built-in in this libcurl due to a build-time decision. This means that a feature or option was not enabled or explicitly disabled when libcurl was built and in order to get it to function you have to get a rebuilt libcurl.

CURLE_COULDNT_RESOLVE_PROXY (5)

Couldn't resolve proxy. The given proxy host could not be resolved.

CURLE_COULDNT_RESOLVE_HOST (6)

Couldn't resolve host. The given remote host was not resolved.

CURLE_COULDNT_CONNECT (7)

Failed to connect() to host or proxy.

CURLE_FTP_WEIRD_SERVER_REPLY (8)

After connecting to a FTP server, li

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值