Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I detect this error using php curl? curl return success even if the URL does not exist.

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

    }
like image 946
Harikrishnan Avatar asked May 20 '13 04:05

Harikrishnan


1 Answers

use this to get HTTP status code:

$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

If 404, it means the request URL cannot be found.

Note: remember to issue this line before curl_close($ch);

like image 156
Raptor Avatar answered Sep 29 '22 01:09

Raptor