2012-05-06 46 views
7

掙扎了半天,我終於設法得到驗證碼通過轉換此功能工作:阻塞fsockopen是什麼?

function _recaptcha_http_post($host, $path, $data, $port = 80) { 

$req = _recaptcha_qsencode ($data); 

$http_request = "POST $path HTTP/1.0\r\n"; 
$http_request .= "Host: $host\r\n"; 
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; 
$http_request .= "Content-Length: " . strlen($req) . "\r\n"; 
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n"; 
$http_request .= "\r\n"; 
$http_request .= $req; 

$response = ""; 
if(false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) { 
    die ("Could not open socket"); 
} 

fwrite($fs, $http_request); 

while (!feof($fs)) 
    $response .= fgets($fs, 1160); // One TCP-IP packet 
fclose($fs); 
$response = explode("\r\n\r\n", $response, 2); 
return $response; 
} 

到:

function _recaptcha_http_post($host, $path, $data, $port = 80) { 
$req = _recaptcha_qsencode ($data); 
$request = curl_init("http://".$host.$path); 

curl_setopt($request, CURLOPT_USERAGENT, "reCAPTCHA/PHP"); 
curl_setopt($request, CURLOPT_POST, true); 
curl_setopt($request, CURLOPT_POSTFIELDS, $req); 
curl_setopt($request, CURLOPT_RETURNTRANSFER, true); 

$response = curl_exec($request); 
return $response; 
} 

基本上,我很感興趣,找出原因curl作品而fsockopen由於「無法打開套接字」而失敗。謝謝。

此外:插座支持已啓用。

+0

stfu運算符(@)將錯誤消除。這使得很難找出問題所在。 – goat

+0

@chris,錯誤是「php_network_getaddresses:getaddrinfo失敗:名稱或服務未知」。任何幫助? –

+0

我的猜測會是你的$ host的值不正確。請參閱fsockopen() – goat

回答

0

什麼在$ errno和$ errstr裏面if(false === ...)?那麼,什麼是它的輸出,如果你改變

if(false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) { 
    die ("Could not open socket, error: " . $errstr); 
} 
+0

它說「php_network_getaddresses:getaddrinfo失敗:名稱或服務未知」 –

+0

<?php echo ini_get('allow_url_fopen'); ?>說? – Alexey

+0

它已啓用。 –

0

哇,

if(false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) { 
    die ("Could not open socket"); 
} 

這並沒有任何意義肯定。請嘗試:

$fs = fsockopen($host, $port, $errno, $errstr, 10); // @ ignores errors 
if(!$fs) die ("Could not open Socket"); 

此外,Skype有時也會阻止端口80。

+1

它確實有道理。在PHP中,賦值的結果是被賦值的值。所以$ fs獲取fsockopen的返回值,然後該值也被測試爲false。這就是$ x = $ y = $ z = 42;''會一次給所有三個變量賦值42。 –

+0

omg,現在我也回答了這個問題,但沒有意識到你碰到了這個16個月的問題:) –

0

Googling因爲你的錯誤導致你想知道你的/etc/resolv.conf是否可以被PHP讀取。在bash中執行ls -lah /etc/resolv.conf以查看它是否可讀。你會得到這樣的:

myserver:~ myname$ ls -lah /ets/resolv.conf 
lrwxr-xr-x 1 root wheel 20B 16 mrt 2011 /etc/resolv.conf 
    ^if there is an 'r' here it is readable. if you have '-' here, it is not. 

如果不是可讀,嘗試做在Bash:chmod 644 /etc/resolv.conf,使之可讀。

+0

是的,它已被其他人閱讀。 –

1

我可能是錯的,但你在fsockopen()使用$port = 80而在捲曲情況下,這個變量不會使用的。當我嘗試連接到SSL通過port 80而不是端口443時我有同樣的問題;據我所知,默認情況下cURL檢查SSL並相應地連接。

此外,請嘗試運行cURLCURLOPT_VERBOSE以查看它的功能。

+0

感謝您的最新答案,但我嘗試使用舊代碼中的端口443。它也行不通。 –