search
HomeBackend DevelopmentPHP TutorialPHP5.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 用的协议都不一样),只能说明是长期稳定的,并不能排除突发事件的影响



好的,谢了
Statement
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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software