2016-07-03 77 views
0

爲什麼strpos PHP不適用於fsockopen響應?爲什麼strpos PHP不能與fsockopen響應一起工作?

當負載此代碼。該代碼將請求sdgsgsdgsfsdfsd.cawhois.cira.ca服務器和查找文本Domain status: availablestrpos PHP如果發現它的將是回聲

{"domain":"sdgsgsdgsfsdfsdca","availability":"available"} 

,但如果沒有找到的文本。它將會呼應

{"domain":"sdgsgsdgsfsdfsdca","availability":"TAKEN"} 

在這種情況下found text但仍然呼應

{"domain":"sdgsgsdgsfsdfsdca","availability":"TAKEN"} 

我該怎麼辦?

<?php 
$server = "whois.cira.ca"; 
$response = "Domain status: available"; 
showDomainResult(sdgsgsdgsfsdfsd.ca,$server,$response); 

function checkDomain($domain_check,$server,$findText) 
{ 
    $con = fsockopen($server, 43); 
    if (!$con) return false; 
    fputs($con, $domain_check."\r\n"); 
    $response = ' :'; 
    while(!feof($con)) 
    { 
     $response .= fgets($con,128); 
    } 
    echo $response."<BR><BR><BR><BR><BR>"; 
    fclose($con); 
    if (strpos($response, $findText)) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

function showDomainResult($domain_check,$server,$findText) 
{ 
    if (checkDomain($domain_check,$server,$findText)) 
    { 
     class Emp 
     { 
      public $domain = ""; 
      public $availability = ""; 
     } 
     $e = new Emp(); 
     $e->domain = $domain_check; 
     $e->availability = "available"; 
     echo json_encode($e); 
    } 
    else 
    { 
     class Emp 
     { 
      public $domain = ""; 
      public $availability = ""; 
     } 
     $e = new Emp(); 
     $e->domain = $domain_check; 
     $e->availability = "TAKEN"; 
     echo json_encode($e); 
    } 
} 
?> 

回答

0
你使用strpos錯

,如果字符串與您要搜索的內容開始,它將返回INT(0),它通過PHP的定義是「有點假」。明確檢查false,這樣

return false!==strpos($response, $findText);

,並確保你使用!==!=

,並作爲一個經驗法則,從來沒有在PHP中使用比較寬鬆運營商,如果你能避免它,如果你這樣做,可能會出現一些可笑的錯誤:https://3v4l.org/tT4l8

相關問題