ID #107

The PHP curl module doesn't work.

Applies to: Grid System

First make sure that you have the CURL php module loaded.

With the CURL module properly loaded, it can behave slightly differently when PHP is running as a cgi than it does when PHP is running as an Apache module.

If the curl script tries to fopen() a file in a directory that is not writable by the curl script, then the curl script will print the curl'ed page to the browser instead.

This means that when running the module version of PHP where scripts run with the permissions of the webserver, it is likely that the webserver may not have write permission in your directory where curl's fopen() is trying to plant a file. The result is that the file gets printed to the browser (which may be what you want).

This will rarely happen with the cgi version because PHP scripts in cgi mode execute with your permissions, and you almost always have write permission in your own directories. Therefore, the curl script will succeed at fopen() and so nothing will be printed to the browser.

If you want the cgi version of PHP to behave like the module version (printing the file to the browser) then just do:

fpassthru ($fp);

after the place where your code uses fopen() and saves the result as $fp

Following the above instruction, the following code will work exactly the same no matter if PHP is being run as a module or as a cgi:

$sapi = php_sapi_name();
echo "SAPI: $sapi

$ch = curl_init("");
$fp = fopen("superprivate_$sapi.txt", "w");

fpassthru ($fp);
// that line makes cgi act like module

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

$curl_exec = curl_exec($ch);

echo "curl_exec: $curl_exec";


Last update: 2010-09-29 16:52
Author: FAQ Admin
Revision: 1.2

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 1 (1 Vote)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ