如何使用php curl檢測此錯誤?即使URL不存在,curl也會返回成功。如何在php curl中檢測到URL未找到錯誤?
Not Found
The requested URL /foo/index.php/items/edit was not found on this server.
我的代碼
$post_fields = array('info' => json_encode($fields));
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($post_fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLINFO_CONTENT_TYPE,'application/x-www-form-urlencoded charset=utf-8');
$result = curl_exec($ch);
echo $result;
if(curl_error($ch)){
$result = curl_error($ch);
curl_close($ch);
return $result;
}else{
curl_close($ch);
return json_decode($result,true);
}
$ HTTP_STATUS = curl_getinfo($ CH,CURLINFO_HTTP_CODE)發出這條線;檢查它是否是404或其他東西 –
找不到URL應該返回404錯誤,所以你應該檢查響應的標題。 – MatRt