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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor