0

I'm making a site crawler/sitemap generator module, that needs to be able to run on CRON. I've finished everything and I'm at the stage where I need to ping search engines my new sitemap file. My module has a web interface and the ping works fine from there, but since it needs to run on CRON I made a CConsoleCommand that runs it.

So, the problem I'm getting is that I'm passing the $_SERVER['SERVER_NAME'] variable to the ping action. And CConsoleCommand is throwing an error on Undefined index: SERVER_NAME.

Now, I've tried:

$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
Yii::app()->request->getBaseUrl(true) //returns absolute URL

But they all throw errors on SERVER_NAME. Does CConsoleCommand not have support for these variables?

C:\xampp\htdocs\YiiCrawler>protected\yiic crawler-sitemap crawl
PHP Error[8]: Undefined index: SERVER_NAME
    in file C:\xampp\htdocs\YiiCrawler\yii-1.1.16.bca042\framework\web\CHttpRequest.php at line 344
#0 C:\xampp\htdocs\YiiCrawler\yii-1.1.16.bca042\framework\web\CHttpRequest.php(392): CHttpRequest->getHostInfo()
#1 C:\xampp\htdocs\YiiCrawler\protected\modules\AvocadoCrawler\commands\ConsoleCrawlerCommand.php(26): CHttpRequest->getBaseUrl()
#2 unknown(0): ConsoleCrawlerCommand->actionCrawl()
#3 C:\xampp\htdocs\YiiCrawler\yii-1.1.16.bca042\framework\console\CConsoleCommand.php(172): ReflectionMethod->invokeArgs()
#4 C:\xampp\htdocs\YiiCrawler\yii-1.1.16.bca042\framework\console\CConsoleCommandRunner.php(71): ConsoleCrawlerCommand->run()
#5 C:\xampp\htdocs\YiiCrawler\yii-1.1.16.bca042\framework\console\CConsoleApplication.php(92): CConsoleCommandRunner->run()
#6 C:\xampp\htdocs\YiiCrawler\yii-1.1.16.bca042\framework\base\CApplication.php(184): CConsoleApplication->processRequest()
#7 C:\xampp\htdocs\YiiCrawler\yii-1.1.16.bca042\framework\yiic.php(33): CConsoleApplication->run()
#8 C:\xampp\htdocs\YiiCrawler\protected\yiic.php(7): require_once()
Aleksandar B.
  • 128
  • 1
  • 9
  • see http://stackoverflow.com/questions/626416/php-server-name-from-command-line – apoq Feb 01 '15 at 16:52
  • @apoq, thanks but not really the answer I was looking for. – Aleksandar B. Feb 03 '15 at 15:45
  • it's impossible to get $_SERVER var from console, that's why you get this error. See http://stackoverflow.com/questions/13951429/get-base-url-in-yii-consol-application – apoq Feb 03 '15 at 19:04

2 Answers2

0

This error goes back to a basic of understanding the uses of PHP, and it's deployment in a web environment, and as a normal PHP script, so I would urge you to review these concepts to get past your current issue.

When you run a PHP script in a web environment, the web server (example Apache) makes available some data, which PHP exposes to your script via the $_SERVER super-global variable.

Basically speaking, when you run a PHP script from the command line, there is no web server, therefore the $_SERVER variable is not available.

To get the server name, use

$myServer = gethostname()

@apoq posted a linked question that has a number of alternatives that you can use. take some time to read that post.

crafter
  • 6,246
  • 1
  • 34
  • 46
  • thanks but for answering, but it's not what I need. I don't need the name of the server, which is returned by `gethostname()` and `php_uname('n')`. I was expecting the `$_SERVER` variable to still be available since the module is still part of a web application and the Yii console apps still use the base Yii framework. – Aleksandar B. Feb 03 '15 at 15:50
  • Please read again< Because the source code sits within the web root directory structure does not mean $_SERVER is available. $_SERVER is made available via the web server, and when you are running a command line script, there is no web server, therefore the server variables are not available. What you are asking for is not available. – crafter Feb 03 '15 at 16:56
0

It's true that running php in console mode means that you do not have available the $_SERVER variables.

In my case I did fake it, force it placing this right at the beginning of the console.php:

$_SERVER['SERVER_NAME']='www.example.com';

Then in components I've also edited the request's hostInfo:

...

// application components
    'components'=>array(

        'request' => array(
            'hostInfo' => (_USE_SSL_?"https://":"http://")."{$_SERVER['SERVER_NAME']}",
            'baseUrl' => "",
            'scriptUrl' => '',
        ),
...

I think you can just skip the $_SERVER['SERVER_NAME'] faking and just define hostInfo and that should make Yii generate urls in console mode. But not sure now, as you see I'm using both.

  • Thanks for the answer, you got close to what I need but I'd rather not hard code it. If there's no other solution I'll implement a config variable which will need to be changed from instance to instance. – Aleksandar B. Feb 03 '15 at 15:52