php脚本在浏览器中工作,但在命令行中出现致命错误

I've got a basic script that I want to run from a cronjob:

require("/opt/lampp/htdocs/cronjobs/aws-sdk-for-php/sdk.class.php");

$amazonSes = new AmazonSES(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY, "certificate_authority" => $CA ));


$response = $amazonSes->send_raw_email(array(
                'Data'=> base64_encode($message)),
                     array('Source'=>$src, 'Destinations'=> $dest));

The above works perfectly when I load the .php from the browser. But from the command line or conjob it returns this fatal error:

Fatal error: Call to undefined function curl_init() in /opt/lampp/htdocs/cronjobs/aws-sdk-for-php/lib/requestcore/requestcore.class.php on line 615

Any ideas?

Your CLI (command line interface) PHP is using a different PHP.INI. When calling PHP from command line, use arguments to make PHP use the same PHP.INI. (see php --help to see the available arguments.)

Or ... configure the alternative PHP.INI that is used by the CLI PHP. (Try opening a php file from CLI that only contains the following code: <?php phpinfo(); ?>. It will tell you which PHP.INI is exactly being used.

Your webserver (apache presumably) is either using a different copy of PHP than the one you use when calling it from the commandline (called CLI), or it is using a different configuration file.

Regardless, you can use phpinfo() and call that on your browser to see the path to your PHP copy which is being used. On the commandline you can call php -ini and look for the path to your PHP copy.

The error you get is because your CLI version of PHP is configured to not incorporate the curl library. Therefore use the commandline to get the loaded configuration file (INI) and use a text editor to edit that file and enable curl.