如何解决Nginx下UCenter通讯失败的问题

2023-05-15,,

本篇文章给大家分享的是有关如何解决NginxUCenter通讯失败的问题,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

解决Nginx下,UCenter通讯失败的问题。  

现象

最后还是“通信失败”,不过跟踪ucenter的代码,在uc_server/model/misc.php的dfopen()函数中,有如下代码:

if(!$fp = @fsocketopen(($scheme == 'https' ? 'ssl' : $scheme).'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)), $port, $errno, $errstr, $timeout)) {
     $context = array(
        'http' => array(
           'method' => $post ? 'POST' : 'GET',
           'header' => $header,
           'content' => $post,
           'timeout' => $timeout,
        ),
     );
     $context = stream_context_create($context);
     $fp = @fopen($scheme.'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)).':'.$port.$path, 'b', false, $context);

在if条件内设置断点,即调用fsocketopen()函数失败的时候,此时查看$errstr变量的内容,其值如下:

Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?

原因

查看fsocketopen函数的代码:

function fsocketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
     $fp = '';
     if(function_exists('fsockopen')) {
        $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
     } elseif(function_exists('pfsockopen')) {
        $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
     } elseif(function_exists('stream_socket_client')) {
        $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
     }
     return $fp;
  }

发现其是调用的fsockopen()函数失败,在网上查找,发现大多数都是说fsockopen()函数不支持“http://”这样的host前缀,要直接用网址,类似www.163.com,或者localhost这样的。

解决

按照网上的说法,我临时修改代码如下:

if(!$fp = @fsocketopen('localhost', $port, $errno, $errstr, $timeout)) {
  //if(!$fp   = @fsocketopen(($scheme == 'https' ? 'ssl' : $scheme).'://'.($scheme ==   'https' ? $host : ($ip ? $ip : $host)), $port, $errno, $errstr, $timeout)) {
     $context = array(
        'http' => array(
           'method' => $post ? 'POST' : 'GET',
           'header' => $header,
           'content' => $post,
           'timeout' => $timeout,
        ),
     );
     $context = stream_context_create($context);
     $fp = @fopen($scheme.'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)).':'.$port.$path, 'b', false, $context);
     $fpflag = 1;
  }

暂时直接将第一个参数固定为localhost,重新刷新页面,终于通信成功:

                                             

知道了真正原因,将上述的临时代码用正确的方式修改就很简单了,这里就不浪费笔墨了。

以上就是如何解决Nginx下UCenter通讯失败的问题,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注本站行业资讯频道。

《如何解决Nginx下UCenter通讯失败的问题.doc》

下载本文的Word格式文档,以方便收藏与打印。