简体   繁体   中英

Why am i getting Undefined index: HTTP_HOST error?

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 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM