Why am i getting Undefined index: HTTP_HOST error? - php

I am using Facebook SDK to post some test wall post on my own facebook page. It works fine when i run the script on my browser but when i run it from terminal it gives me as error as below, i don't know what's wrong please help. I want to post on my facebook page using php CRON scripts like every 6 hours.
Undefined index: HTTP_HOST error in Facebook/src/base_facebook.php

The cron executes the PHP not like a module of apache, so many environment variables are not set by the server. When executing from cron your PHP script is like GCI one, more precisely its CLI (command line interface - php-cli). So as you can imagine, there is no web server and there is no HTTP_HOST.
PS: You can transfer data (urls, hostname or whatever you like) as command line arguments (environment variables) to PHP: Command line usage
Addition:
$php -f cronjob.php HTTP_HOST=www.mysite.com #example
<?php
// cronjob.php
$host = $_GET['HTTP_HOST']; // Get the host via GET params
?>

If you run your script from a terminal, or a cron job, there is no HTTP environment.
A possible solution to this is to run the script with a wget http://.../parameters instead of with php scriptname.

Related

Call to undefined function mysqli_connect() [Cron job only]

When running a php script by navigating to the file directly on the server (via the web), everything runs fine.
However, when setting up the same file to run via a cron job, I get the error message:
Call to undefined function mysqli_connect()
The cron job is set up to run this file, with permission 744:
php -q /home/username/public_html/seafood/php/lobsteremail.php
Any idea why a file might run fine manually but not as part of a cron job?
Is your cron job set to run on boot?
If so, it cant find mysqli_connect() because mysql is not running yet.
Before calling the php script, you need to wait.
Sleep 10 # wait for mysql
php -q /home/username/public_html/seafood/php/lobsteremail.php

Tell PHP to enable or use GD when running script via cron

I am attempting to run a php script via cron and I'm hitting a brick wall.
When I run the php script via the command line as root, everything works correctly.
When I run the php script via the command line as the user, everything works correctly.
The error that I am getting is:
PHP Fatal error: Call to undefined function imagecreatefromstring()
To test I created a php file that has...
<?php
var_dump(gd_info());
?>
When that file is executed via cron it again says that I have a fatal error. "Call to undefined function gd_info()"
So at this point, I've narrowed it down to GD not loading in the cron environment, but at this point, I don't know what to do to fix the issue.
My guess is you have multiple copies of PHP. You and root use one built with GD, cron uses another due to PATH environment variable inconsistencies.
As a working user (you or root), run
which php
That will give you a full path like /usr/bin/php. Use that path in your cron entry, eg
0 0 * * * /usr/bin/php /path/to/your/script.php

Crontab on AWS EC2 and $_SERVER PHP variables

I have some PHP scripts for database maintenance in my server that requires its periodically execution. Obviously the easiest solution is to schedule its running with system cron.
The scripts require some server variables accessed from $_SERVER, like database hostname, cron parameters, etc.
I can run the scheduled cron commands from command line without any problem, and everything seems to be working fine (calling something like php filename.php). However, when the same commands are executed from cron, the scripts fails and the error reported is like the following:
PHP Notice: Undefined index: RDS_DATABASE in
/var/app/current/app/xx/Db/ConnectionFactory.php on line 8 PHP
Seems that the $_SERVER variable is not correctly initialized when running from cron, but it works from command line. I have tried with crontab -u ec2-user -e but without luck.
I do not want to use wget to run the script as it adds some overhead, and the scripts are hidden from being accessed from HTTP.
Any hint about successfully accessing $_SERVER from command line, but failing when running from crontab?
Had the same issue. Found a solution:
echo "InstanceID: ".get_cfg_var('INSTANCE_ID')."\n";
For some reason it was working fine on my ec2 user but not as a root cron job. Using the function instead of accessing the $_SERVER array solved my problem.
$_SERVER only works if you will run PHP using any web server. If you will use crontab and execute PHP via command line it will not work. You may refer to PHP documentation http://php.net/manual/en/reserved.variables.server.php#refsect1-reserved.variables.server-indices
As #Baminc and #Ankur says the solution is to use get_cfg_var function to get the information because $_SERVER only works when you access it from a web browser.
What I do is the following for example with SERVER_NAME :
if (isset($_SERVER['SERVER_NAME'])) {
$myServerName = $_SERVER['SERVER_NAME'];
} else {
$myServerName = get_cfg_var('SERVER_NAME');
}
Hope this helps!

cURL not working in Windows Task Scheduler

I've a PHP script that uses cURL to perform certain tasks. At the moment, I have the script running every 10 minutes. This is what I'm running via Windows Task Scheduler.
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\autoscripts\index.php
However, for some reason, whenever the argument quoted above is run through the command line, I get the error "Fatal error: Call to undefined function curl_init()". The script works perfectly when I access it via the browser. Is there any reason why PHP isn't able to access the cURL extension via the command line?
Most likely running from command line does not use any ini file that loads the extensions. Open phpinfo() from the browser, copy path to loaded ini file and change your task to:
C:\wamp\bin\php\php5.4.3\php.exe -c "C:\path\to\php.ini" -f C:\wamp\www\autoscripts\index.php
Figured it out. Basically, on WampServer, there are TWO php.ini files that you need to be aware of.
C:\wamp\bin\php\php5.4.3\php.ini
C:\wamp\bin\apache\apache2.2.22\bin\php.ini
Forgot that the command line uses a different ini file than the web server. :(

cURL doesn't work from command line?

I wrote a script to parse some data from a website using cURL and it works fine when I run it in my browser, however when I want to run it in the command line I get the error "call to undefined function curl_init()". Do php scripts run under different settings from the command line?
This is happening because you are simply trying to call a PHP function from bash. If you have curl installed in your linux environment then the command should simply be curl [-options] [url]. The simplest of them being something like:
$ curl http://someurl.com/path/to/xmlfile.xml
You can test for this from the command line by tying "$ which curl" (without the quotes of course). That will give you the path to where it is stored in case you have to use the full path. (e.g. /usr/bin/curl [-options] [url]).
EDIT:
after having re-read your question I realized that I dumbly missed the fact that you said you were trying to run the PHP script from the command line and not curl itself. And now I, too, am stumped by your problem. Sorry!

Categories