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>

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 03:10:33
Script start
Timing: Jan 18 2025 03:10:33
Module start 'layout'
Timing: Jan 18 2025 03:10:33
Module start 'content'
Timing: Jan 18 2025 03:10:34
Module end 'content'
Timing: Jan 18 2025 03:10:34
Script end

Main resources:

Total runtime0.6771 sec
Peak memory usage4,096.0000 KB
Database Queries59

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0050 591.8047152.6563
Module start 'layout' 0.00500.0031 744.460939.5156
Module start 'content' 0.00810.6676 783.9766549.1484
Module end 'content' 0.67570.0013 1,333.125012.4063
Script end 0.6770  1,345.5313 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00290.4240160.0002
Check MTime0.00120.1798160.0001
Mysql Total
Database connection0.00080.125410.0008
Mysqli_queries0.621191.7278590.0105
Looping result0.00050.0804570.0000
Template Total0.646695.520.3233
Template load0.00200.300820.0010
Template processing0.644695.196120.3223
Template load and register function0.00020.025110.0002
states
state_id_array0.00090.128710.0009
state_identifier_array0.00180.267320.0009
Override
Cache load0.00180.2616430.0000
Sytem overhead
Fetch class attribute can translate value0.00070.100620.0003
Fetch class attribute name0.00110.156640.0003
XML
Image XML parsing0.00060.091720.0003
class_abstraction
Instantiating content class attribute0.00000.001440.0000
General
dbfile0.00170.2537180.0001
String conversion0.00000.001340.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
7content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 17
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs