Unable to find the socket transport "http"

解決Nginx下,UCenter通信失敗的問題。  

現象

上一篇文章《Nginx下,請求本機另外Host很慢》中,到最後仍是「通訊失敗」,不過跟蹤ucenter的代碼,在uc_server/model/misc.phpdfopen()函數中,有以下代碼:php

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);socket

 

if條件內設置斷點,即調用fsocketopen()函數失敗的時候,此時查看$errstr變量的內容,其值以下:ide

 

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

緣由

查看fsocketopen函數的代碼:post

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;
 
}spa

 

發現其是調用的fsockopen()函數失敗,在網上查找,發現大多數都是說fsockopen()函數不支持「http://」這樣的host前綴,要直接用網址,相似www.163.com,或者localhost這樣的。orm

解決

按照網上的說法,我臨時修改代碼以下:server

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;
 
}blog

 

暫時直接將第一個參數固定爲localhost,從新刷新頁面,終於通訊成功:ip

image.png                                             

 

知道了真正緣由,將上述的臨時代碼用正確的方式修改就很簡單了,這裏就不浪費筆墨了。

相關文章
相關標籤/搜索