加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在php curl中检测URL Not Found Error?

发布时间:2020-12-13 13:27:56 所属栏目:PHP教程 来源:网络整理
导读:如何使用php curl检测此错误?即使URL不存在,curl也会返回成功. Not FoundThe 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);
如何使用php curl检测此错误?即使URL不存在,curl也会返回成功.
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状态代码:
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

如果是404,则表示无法找到请求URL.

注意:记得在curl_close($ch)之前发出这一行;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读