PHP5.6 CURL 超时问题,请大侠帮忙分析下原因
最近做微信开发,CURL访问微信接口总是莫名的出现超时问题,各种尝试,以下配置方式,相对稳定一些,但是每天还是会出现超时情况,CURL设置如下:
$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。 curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 0); // 设置在内存中缓存 DNS 的时间,默认为120秒(两分钟)。 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。 curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); //TRUE 强制获取一个新的连接,而不是缓存中的连接。 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 200); //尝试连接等待的时间,以毫秒为单位。设置为0,则无限等待。curl_setopt($curl, CURLOPT_TIMEOUT_MS, 800); //设置cURL允许执行的最长毫秒数if($https==1){ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);}$gf = curl_getinfo($curl);$data = curl_exec($curl);curl_close($curl);
返回的错误信息是:SSL connection timeout
打印curl_getinfo($cul)获得信息如下
url=https://api.weixin.qq.com/cgi-bin/user/info?access_token=_lAyXxUpGaDZxV&openid=osPXyjh7a8L&lang=zh_CNcontent_type=http_code=0header_size=0request_size=0filetime=-1ssl_verify_result=1redirect_count=0total_time=0.063namelookup_time=0.032connect_time=0.063pretransfer_time=0size_upload=0size_download=0speed_download=0speed_upload=0download_content_length=-1upload_content_length=-1starttransfer_time=0redirect_time=0redirect_url=primary_ip=140.207.135.108certinfo=Arrayprimary_port=443local_ip=196.205.247.126local_port=57982
操作系统为 win2012 r2,php5.6
从浏览器打开出错的URL,一切正常。
请大侠帮忙分析下什么原因造成的,
回复讨论(解决方案)
适当加大超时时间,800ms 显然是短了点
或参考这篇博文的做法 http://www.sitecrafting.com/blog/php-curl-ssl-connection-timeout/
超时时尝试重新链接
适当加大超时时间,800ms 显然是短了点
或参考这篇博文的做法 http://www.sitecrafting.com/blog/php-curl-ssl-connection-timeout/
超时时尝试重新链接
按照这篇测试过,也尝试过将链接时间和超时时间设置的久一点,但是依然会出现问题。
适当加大超时时间,800ms 显然是短了点
或参考这篇博文的做法 http://www.sitecrafting.com/blog/php-curl-ssl-connection-timeout/
超时时尝试重新链接
按照这篇测试过,也尝试过将链接时间和超时时间设置的久一点,但是依然会出现问题。
在链接失败之后,无论是重新执行curl_init(),还是执行curl_reset(),都会提示相同的错误。
你的代码,出现错误就停止了
而他示例的代码,尝试 10 次后才结束
你说按他的方法测试过,但你给出的代码并没有表现出
连接超时本来就是个很常见的事情(用浏览器时,刷新一下也就好了),写程序时也要模拟一下这个刷新动作
你的代码,出现错误就停止了
而他示例的代码,尝试 10 次后才结束
你说按他的方法测试过,但你给出的代码并没有表现出
连接超时本来就是个很常见的事情(用浏览器时,刷新一下也就好了),写程序时也要模拟一下这个刷新动作
不好意思,上面代码是我精简过的代码,出现错误后我只进行了一次重连操作,由于不确定问题所在(可能是dns解析失败),如果还有错误,会使用另外一个链接分别隔2、4、6秒再次发起起请求。
你的代码,出现错误就停止了
而他示例的代码,尝试 10 次后才结束
你说按他的方法测试过,但你给出的代码并没有表现出
连接超时本来就是个很常见的事情(用浏览器时,刷新一下也就好了),写程序时也要模拟一下这个刷新动作
不好意思,上面代码是我精简过的代码,出现错误后我只进行了一次重连操作,由于不确定问题所在(可能是dns解析失败),如果还有错误,会使用另外一个链接分别隔2、4、6秒再次发起起请求。
重连一次的代码如下
$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);//curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect: '));//curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。 curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 0); // 设置在内存中缓存 DNS 的时间,默认为120秒(两分钟)。 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。 curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);//开启CURLOPT_FAILONERROR选项,必须同时开启CURLOPT_HEADER选项,//否则会造成size_download 和 download_content_length 大小不一致,导致最终无结果输出//curl_setopt($curl, CURLOPT_FAILONERROR, 1); //当 HTTP 状态码大于等于 400,TRUE 将将显示错误详情。 默认情况下将返回页面,忽略 HTTP 代码。 //开启CURLOPT_HEADER选项,需要在输出结果中分离出头部和内容部分,可使用curl_getinfo获取头部大小,进行字符串裁切//curl_setopt($curl, CURLOPT_HEADER, 1); //启用时会将头文件的信息作为数据流输出。curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); //TRUE 强制获取一个新的连接,而不是缓存中的连接。 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 200); //尝试连接等待的时间,以毫秒为单位。设置为0,则无限等待。curl_setopt($curl, CURLOPT_TIMEOUT_MS, 800); //设置cURL允许执行的最长毫秒数curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);$data = curl_exec($curl);$gf = curl_getinfo($curl);if($gf['http_code']==0){ curl_reset($curl); if(curl_errno($curl)){ echo " CURL_Error 2: " . curl_error($curl).'```'.$url; }}curl_close($curl);
我想知道的是,总体执行时间并没有超过我设置的超时限制的情况下,为什么会出现 SSL connection timeout。
我想使用证书验证一下,但是此处的微信接口并没有提供证书文件,不晓得该怎么获取证书。
因为你的 url 是 https://.... 所以链接超时的信息是 SSL connection timeout
如果你的 url 是 http://....的话, 所以链接超时的信息是 HTTP connection timeout
这又不是你的代码的问题,知道了连接超时的具体原因,也没有用,因为不在你的管辖范围
你只能被动的反复尝试连接,直到成功
因为你的 url 是 https://.... 所以链接超时的信息是 SSL connection timeout
如果你的 url 是 http://....的话, 所以链接超时的信息是 HTTP connection timeout
这又不是你的代码的问题,知道了连接超时的具体原因,也没有用,因为不在你的管辖范围
你只能被动的反复尝试连接,直到成功
很抱歉,使用循环链接的方式,不能成功,下面是我循环100链接的代码和输出结果
最新的如果碰到错误,循环执行十次代码,如下
$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。 curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 0); // 设置在内存中缓存 DNS 的时间,默认为120秒(两分钟)。 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。 curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); //TRUE 强制获取一个新的连接,而不是缓存中的连接。 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 200); //尝试连接等待的时间,以毫秒为单位。设置为0,则无限等待。curl_setopt($curl, CURLOPT_TIMEOUT_MS, 800); //设置cURL允许执行的最长毫秒数curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);$data = curl_exec($curl);$gf = curl_getinfo($curl);if($gf['http_code']==0){ for($i=0;$i<100;$i++){ curl_reset($curl); if(curl_errno($curl)){ get_error($do." CURL_Error ".($i+2).": " . curl_error($curl).'```'.$url); }else{ break; } } }curl_close($curl);
执行结果如下
2016-05-20 09:39:39CURL_Error: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 2: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 3: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 4: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 5: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 6: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 7: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 8: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 9: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 10: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code2016-05-20 09:39:39CURL_Error 11: SSL connection timeout```https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9964f7c0&secret=f0ab3a4e974d010fe&code=041wv2q1w6x2t&grant_type=authorization_code...........................
我原以为你是知道 curl 是不能重入的,结果你并不知道
你这样的写法当然不行!
我原以为你是知道 curl 是不能重入的,结果你并不知道
你这样的写法当然不行!
请问,不能重入 是什么意思,需要重新从 curl_init(),开始循环执行么?
对!循环中需要从 curl_init() 开始
只有在 url、请求类型 发生变化时,才可以复用 curl 实例
再说 微信 的 token 是实时发放的,也没有看到你获取的代码
对!循环中需要从 curl_init() 开始
只有在 url、请求类型 发生变化时,才可以复用 curl 实例
谢谢,我再测试下,稍后我将测试结果反馈回来。
再说 微信 的 token 是实时发放的,也没有看到你获取的代码
经过将近半天的测试,首次失败后,重新链接,第二次就链接成功了,暂时没有出现两遍以上没有链接成功的情况。
造成链接失败的原因是因为 CURL不稳定么?前段时间不知道是没有注意还是别的情况,没有发现链接失败的问题,突然有一天就出现这个问题了。
ping 微信接口,时间都是 50毫秒之内。ping 10000次,丢包率为0,。应该不是网络问题造成的链接失败原因,好郁闷。
不能什么事情都赖自己,你怎么就知道不是网络或腾讯的问题呢?(这两天腾讯游戏都是一卡一卡的)
你的那些测试都取的平均值(ping 用的协议都不一样),只能说明是长期稳定的,并不能排除突发事件的影响
不能什么事情都赖自己,你怎么就知道不是网络或腾讯的问题呢?(这两天腾讯游戏都是一卡一卡的)
你的那些测试都取的平均值(ping 用的协议都不一样),只能说明是长期稳定的,并不能排除突发事件的影响
好的,谢了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
