Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what would cause curl to return false when trying to access a local file?

Tags:

php

curl

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.

like image 485
codescribblr Avatar asked Sep 10 '12 20:09

codescribblr


People also ask

How do you catch errors in cURL?

If you want to fetch the error message, make sure you fetch it before you close the current cURL session or the error message will be reset to an empty string. This is returned if CURLOPT_FAILONERROR is set TRUE and the HTTP server returns an error code that is >= 400.

Why PHP cURL is not working?

cURL is supported by your hosting company/plan but not enabled: If cURL is supported by you hosting company but it is not enabled by default, then often you simply just need to login to your hosting dashboard, navigate to the relevant section and enable it. Done!

Can I do a cURL request to the same server?

We cannot use include because the files that we are calling usually call different databases or have functions with the same name.

What is cURL exec?

curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.


2 Answers

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

like image 76
Alain Tiemblo Avatar answered Oct 10 '22 12:10

Alain Tiemblo


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/>';
like image 44
coder618 Avatar answered Oct 10 '22 12:10

coder618