PHP curl 获取响应的状态码的方法

PHP curl可以从服务器端模拟一个http请求,例如抓取网页、模拟登陆等。根据选项设置,可以在curl_exec的返回结果中获取到响应头和body,但这没有响应的状态吗。想要获取状态码,需要在执行curl_exec后再通过curl_getinfo来获取。例如: 

$ch = curl_init (); 
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com.hk'); 
curl_setopt($ch, CURLOPT_TIMEOUT, 200); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_NOBODY, FALSE); 
#curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_exec($ch); 
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); 

 

你可能感兴趣的:(php)