PHP uses curl to get http link content response timeout problem

curl_exec execution is too slow, IPv6 to blame

 

The company website uses WeChat payment to obtain WeChat code and openid value to initiate WeChat payment, but since the company's server upgrade (mainly upgrading the Linux system kernel), it is found that the process of initiating WeChat payment is very slow, and timeouts often occur.

 

So I checked the system code item by item and found that there was a delay in the following code in php

 

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		fwrite($f,"GetOpenidFromMp step 1,".date("H:i:s")."\r\n");
		/ / Run curl, the result is returned in the form of jason
		$res = curl_exec($ch);
		fwrite($f,"GetOpenidFromMp step 2,".date("H:i:s")."\r\n");

 

After the system output, it is found that the time difference between step1 and step2 is 10-20 seconds long. The problem is definitely with the https link

PHP has been set up to filter https certificates and other checks, it should not be stuck

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);

 

With the help of operation and maintenance:

Execute the command in linux:

$ wget  https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxxxx&secret=a651xxxx727d9a5fxxxxxxxc&code=071pxxxxxxxxxSs60QjJw60pIAwV&grant_type=authorization_code

 

It was found that getting results was slow.

If you change to ipv4

$ wget -4  https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxxxx&secret=a651xxxx727d9a5fxxxxxxxc&code=071pxxxxxxxxxSs60QjJw60pIAwV&grant_type=authorization_code

 The result is returned in an instant. It seems that the operation and maintenance has enabled IPv6 addresses. After the entire system is upgraded, IPv6 will be resolved first by default. If the domain does not have IPv6, it will wait for the timeout of IPv6 resolution failure before searching for IPv4 according to the previous normal process.
For PHP curl, just add the following sentence to solve the delay problem:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

Many servers now have IPv6 turned on but no routing, so they can't really work, but lead to some unforeseen problems.

If you also encounter such an inexplicable problem, try it out, and good luck!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616700&siteId=291194637
Recommended