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

现象

上一篇文章《Nginx下,请求本机另外Host很慢》中,到最后还是“通信失败”,不过跟踪ucenter的代码,在uc_server/model/misc.phpdfopen()函数中,有如下代码:

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,重新刷新页面,终于通信成功:

Unable to find the socket transport                                              

 

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