简体   繁体   English

如何检测php curl中的URL Not Found错误?

[英]How can I detect URL Not Found Error in php curl?

How can I detect this error using php curl? 如何使用php curl检测此错误? curl return success even if the URL does not exist. 即使URL不存在,curl也会返回成功。

Not Found
The requested URL /foo/index.php/items/edit was not found on this server.

My code 我的代码

$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);

    }

use this to get HTTP status code: 使用它来获取HTTP状态代码:

$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

If 404, it means the request URL cannot be found. 如果是404,则表示无法找到请求URL。

Note: remember to issue this line before curl_close($ch); 注意:记得在curl_close($ch);之前发出这一行curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM