php

CentOS 7 php curl 微信支付接口 超时

Posted by Sunday on 2019-02-13

事件

  1. 添加了H3C路由器做内网上网使用,节省IP做集群IP.
  2. 调用微信接口 https://api.mch.weixin.qq.com/pay/unifiedorder 超时5秒多。
  3. php报错:cURL error 28: Resolving timed out after 5516 milliseconds (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
    1
    2
    3
    4
    5
    (1/1) ConnectException
    cURL error 28: Resolving timed out after 5516 milliseconds (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

    in CurlFactory.php line 185
    at CurlFactory::createRejection(object(EasyHandle), array('errno' => 28, 'error' => 'Resolving timed out after 5516 milliseconds', 'url' => 'https://api.mch.weixin.qq.com/pay/unifiedorder', 'content_type' => null, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 5.515195, 'namelookup_time' => 0.0, 'connect_time' => 0.0, 'pretransfer_time' => 0.0, 'size_upload' => 0.0, 'size_download' => 0.0, 'speed_download' => 0.0, 'speed_upload' => 0.0, 'download_content_length' => -1.0, 'upload_content_length' => -1.0, 'starttransfer_time' => 0.0, 'redirect_time' => 0.0, 'redirect_url' => '', 'primary_ip' => '', 'certinfo' => array(), 'primary_port' => 0, 'local_ip' => '', 'local_port' => 0))

排查

  1. 使用IDC 外网IP curl 正常
  2. 使用H3C 内网IP curl 超时5秒多

修改resolv.conf

添加options single-request-reopen

1
2
3
4
5
6
cat << EOF> /etc/resolv.conf
options single-request-reopen

nameserver 114.114.114.114
nameserver 119.29.29.29
EOF

禁用IPV6

1
2
3
cat /etc/sysconfig/network

NETWORKING_IPV6=no
1
2
3
4
5
6
cat /etc/sysconfig/network-scripts/ifcfg-xxx

DNS1=114.114.114.114
DNS2=119.29.29.29
IPV6INIT=no
IPV6_AUTOCONF=no

禁用NetworkManager

1
2
systemctl disable NetworkManager 
systemctl stop NetworkManager

修改前

1
2
3
4
5
6
7
8
9
10
11
12
13
time curl -I https://api.mch.weixin.qq.com/pay/unifiedorder
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 13 Feb 2019 08:16:17 GMT
Content-Type: text/plain
Content-Length: 110
Connection: keep-alive
Keep-Alive: timeout=8


real 0m5.75s
user 0m0.062s
sys 0m0.052s

修改后

1
2
3
4
5
6
7
8
9
10
11
12
13
time curl -I https://api.mch.weixin.qq.com/pay/unifiedorder
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 13 Feb 2019 08:16:17 GMT
Content-Type: text/plain
Content-Length: 110
Connection: keep-alive
Keep-Alive: timeout=8


real 0m0.255s
user 0m0.052s
sys 0m0.042s

若开启NetworkManager防止resolv被重置

  1. 创建一个脚本。
1
2
3
4
5
6
cat << EOF > /etc/NetworkManager/dispatcher.d/15-resolv
#!/bin/bash
# Description : script to override default resolv.conf file
# with customized file.
cp -f /etc/resolv.conf.custom /etc/resolv.conf
EOF
  1. 设置文件权限
1
chmod u+x /etc/NetworkManager/dispatcher.d/15-resolv
  1. 创建一个文件
1
2
3
4
vi /etc/resolv.conf.custom
options single-request-reopen
nameserver 114.114.114.114
nameserver 119.29.29.29
  1. 重启服务
1
service NetworkManager restart

https://www.jianshu.com/p/d5b913783d4f
http://www.man7.org/linux/man-pages/man5/resolver.5.html