Forums / Developer / how to use ezurl() equivalent in a php script (launched by CLI...)

how to use ezurl() equivalent in a php script (launched by CLI...)

Author Message

Jerome Despatis

Tuesday 28 June 2011 10:52:40 am

Hello,

I've a small script that needs to print the full url to a file (full url means including the http://domain.com).

I need, in fact, to perform the same operation (in php) as the template operator ezurl(), so I use eZURI::transformURI.

Here are the 2 lines of code:

$url = "/extension/djgfxstock/design/standard/images/foo.png";
eZURI::transformURI($url, false, 'full');

This piece of code perfectly works if I put it in a module/view, but the $url keeps unchanged if I use those 2 lines in a php scriptthat  I launch with php cli, or even with runcronjobs...

The solution would be (maybe) to force the load of a siteaccess, but even with that, I'm not sure it would work...

(indeed runcronjobs loads a siteaccess, and even with that, impossible to use absolute url)

The only solution I've found (which is crappy at a very high level) is to fill in, before my 2 lines, $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_PORT']

Any idea is welcome!

Marko Žmak

Wednesday 29 June 2011 3:52:32 am

The eZURI::transformURI() method, when used with 'full' parameter gets the hostname from the server HTTP_* variables.

Since this variables are not available when runing cli scripts (this is a restriction of Apache/PHP and not eZP), you cannot get the hostname in such a case.

One soultion would be to get the hostname from an INI setting.

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Jerome Despatis

Friday 01 July 2011 12:59:33 am

Exactly, from INI settings

But, even by using ini settings, it's impossible to use eZURI::transformURI(), as it checks only server variables

As a result, I've added a feature request:

http://issues.ez.no/IssueView.php?Id=18423

Maybe it would be interesting to have ?

Because, the only way I've found for now to make ezurl() and eZURI::transformURI() work with 'full' parameter is to add a very ugly hack:

<code>

 if (eZSys::isShellExecution())
        {
            if (!eZSys::serverVariable('HTTP_HOST', true))
            {
                $siteIni = eZINI::instance();
                // Won't work if SiteURL contains other things than just hostname...
                eZSys::setServerVariable('HTTP_HOST', $siteIni->variable('SiteSettings', 'SiteURL'));
            }
            if (!eZSys::serverVariable('SERVER_PORT', true))
            {
                eZSys::setServerVariable('SERVER_PORT', 80);
            }
        }

</code>