CURL问题集锦

技术库 小李 1824浏览
  • cURL error 60: SSL certificate problem: certificate has expired

1、报错原因:因为没有配置信任的服务器HTTPS验证。默认情况下,cURL被设为不信任任何CAs,因此浏览器无法通过HTTPs访问你服务器。

2、解决方法:

进入 https://curl.se/docs/caextract.html 下载https://curl.se/ca/cacert.pem

php配置路径替换  curl.cainfo = /etc/pki/tls/certs/ca-bundle.crt

顺着路径找到文件替换 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem

  • 为什么本地开发时使用CURL请求本地URL会卡死

出现以上原因是因为windows 下 nginx+php环境,不支持并发的原因。
当同时访问多个域名,并且同时指向你本地服务的时候,就不支持并发了。

默认时启动phpcgi是

D:\php \php-cgi.exe-b 127.0.0.1:9000 -c D:\phpfind\phpa\php.ini

先看NGINX配置

1
2
3
4
5
6
7
8
9
       location ~ \.php(.*)$  {
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           fastcgi_param  PATH_INFO  $fastcgi_path_info;
           fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
           include        fastcgi_params;
       }

NGINX中,看PHP文件块fastcig-pass的设置值(127.0.0.1:9000)。设置都是以keepalive方式请求,接收到PHP文件时,交于后端过程PHPCGI解析处理(127.0.0.1:9000),等待响应。而在本地文件以CURL请求本地环境中PHP文件时,之前的PHP还在等待CURL后的结果,这时9000端口已经被占用。导致CURL一直在处于等待状态。不设置timeout超时,程序就会卡死。结果都是false

解决方案:

新开启一个phpcgi进程设置不同端口:

例D:\php\php-cgi.exe -b 127.0.0.1:9001 -c D:\phpfind\phpa\php.ini

在需要被CURL的端口或域名设置中设置。

1
2
3
4
5
6
7
8
9
       location ~ \.php(.*)$  {
           fastcgi_pass   127.0.0.1:9001;
           fastcgi_index  index.php;
           fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           fastcgi_param  PATH_INFO  $fastcgi_path_info;
           fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
           include        fastcgi_params;
       }

这样就可以请求了。但是不能请求同一个域下的文件。

这样可以在nginx中使用php-cgi负载均衡:

1
2
3
4
5
6
7
8
9
10
11
12
13
        upstream backend{
            server 127.0.0.1:9000;
            server 127.0.0.1:9001;
        }
        location ~ \.php(.*)$  {
            fastcgi_pass   backend;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

见效果:

wKiom1R34OaQKrP9AAIa2TRYdYU845.gif

转载请注明:清韵逸-博客生活分享 » CURL问题集锦