Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:09:48+00:00 2026-06-02T07:09:48+00:00

I am using cURL to retrieve a file from a server, I wonder is

  • 0

I am using cURL to retrieve a file from a server, I wonder is there a way to specify a return value when curl_exec() fails. My code is like this:

if (!$result = curl_exec($curl)){
    echo "something is wrong.";
    exit;
}
/**
 *the rest of the script
 */

When the exec() call fails, the echo statement is not printed on the page, and apparently the exit() is not called as well, causing my following scripts to execute as normal. I noticed that when curl_exec() fails, it does not give you FALSE, it just gives you a something like “NOT FOUND” page, I guess this is where my problem is. Is there any way that can force it to return a boolean or a string so that I can do the condition test? Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:09:49+00:00Added an answer on June 2, 2026 at 7:09 am

    curl_exec will only return false if the HTTP request did not complete successfully from a technical standpoint. If the request itself does complete but the result is not what you expected (e.g. the server responded with a 404) it will correctly return true or the result (depending on CURLOPT_RETURNTRANSFER being set or not).

    Therefore you have to check for this eventuality separately. For example:

    if (!$result = curl_exec($curl)){
        echo "request totally failed.";
        exit;
    }
    
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ($code >= 400) {
        echo "the server responded with an error code.";
        exit;
    }
    

    See the list of HTTP status codes for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to retrieve data from an api using curl with this code: $xml_data
I'm using cURL to retrieve a webpage. What's the easiest way to get encoding
I'm using PHP+curl to retrieve an XSL file, but first I want to do
The following code will retrieve the body content of a url retrieved using CURL
I want to retrieve the first 10k bytes from a URL with curl (using
I am using PHP curl to retrive an xml file from a remote url
I can upload a file to my Apache web server using Curl just fine:
I am trying to retrieve some data from an API using CURL request. Is
I'm using cURL to get the XML file for my Twitter friend's timeline. (API
Answer : Implemented using Curl... $file = http://abc.com/data//output.txt; $ch = curl_init($file); $fp = @fopen(out.txt,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.