21

This site has been up for several months now and has been working fine. I have a PHP page that creates an invoice from data in the url (e.g. viewinvoice.php?id=250 builds an invoice based on record 250). This page is accessible via a web browser and works fine.

On a completely different page (i.e. test.php) I'm trying to access that file via cURL. However, when I make the call and var_dump the results, I get bool(false).

Here's the function that makes the cURL call:

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

HOME is a constant that denotes the full url (e.g. http://www.example.com/).

$invoice_contents = file_get_contents_curl(HOME.'viewinvoice.php?id=242');
echo $invoice_contents;
var_dump( $invoice_contents );

I've tried changing the url to an external url (i.e. http://www.google.com/) and the page loads just fine. I get google's home page. But any page that's in the same domain won't load. Is there a reason that this would happen?

I am not the server admin, but I have root access to the server. I have not changed any settings recently, but the server admin may have upgraded the version of apache or php?

In any case, is there a setting I can modify to make this work again?

P.S. I just tried making this exact call from an external server (different domain) and it works just fine.

Cœur
  • 37,241
  • 25
  • 195
  • 267
codescribblr
  • 1,146
  • 2
  • 11
  • 29

3 Answers3

57

After your curl execution, put something like this :

echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';

You'll see what failed during your curl execution.

More info : curl_getinfo curl_errno curl_error

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
  • 2
    I added these lines just before curl_close($ch) and was able to see more about the request. I'll definitely be using this approach in the future. Unfortunately, the problem solved itself before I got to trying this, so there were no errors present. The site went back to working as it did before. I never thought I'd say that I wish it was still broken, but since I have no idea what went wrong...I wish it was. If it happens again, I'll be sure to use this info. – codescribblr Sep 10 '12 at 22:33
2

After execution put this variable to test your errors

echo "<pre>";
var_dump( curl_getinfo($ch) ) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
coder618
  • 848
  • 1
  • 9
  • 12
0

I had a similar problem where PHP curl returned false. The accepted answer dosent solve the problem.

My case: I have a website which makes PHP curl calls to my private server to get data. It worked fine for 2 years but suddenly curl started to return false.

  1. I tried to open my private server page in browser and it was working fine.

  2. I tried PHP file_get_contents("http://my.server/page.php"); and was also working fine.

But curl was still returning false.


For debugging PHP curl I found a much better solution here: Php - Debugging Curl

The problem: My ISP had issues with network and my internet connection speed dropped from 100Mbps to 1.5Mbps at that time - The PHP curl did not spend enough time to wait for the response.


The solution: I added this 2 lines in curl calls:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); //timeout in seconds

Which I found here: Setting Curl's Timeout in PHP

Cyborg
  • 1,437
  • 19
  • 40