如果你连接到服务器,那么你可以从它得到一个返回码,否则它会失败,你会得到一个0.所以,如果你尝试连接到“www.google.com/lksdfk”你会得到一个返回码400,如果你直接去google.com,你会得到302(如果你转到下一个页面,那么你会得到200 …呃,我这样做,因为它转发到google.com.br,所以你可能不会那么做),如果你去“googlecom”你会得到一个0(主机没有find),所以最后一个,没有人发回代码。
使用下面的代码进行testing。
<?php $html_brand = "www.google.com"; $ch = curl_init(); $options = array( CURLOPT_URL => $html_brand, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_MAXREDIRS => 10, ); curl_setopt_array( $ch, $options ); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ( $httpCode != 200 ){ echo "Return code is {$httpCode} \n" .curl_error($ch); } else { echo "
".htmlspecialchars($response).""; } curl_close($ch);