Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am i getting Undefined index: HTTP_HOST error?

Tags:

php

facebook

cron

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

like image 889
Keyur Padalia Avatar asked Apr 10 '26 19:04

Keyur Padalia


2 Answers

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
?>
like image 153
Rolice Avatar answered Apr 12 '26 10:04

Rolice


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.

like image 22
JvdBerg Avatar answered Apr 12 '26 10:04

JvdBerg