Not sure if the title is correct, Please edit if you think of a better one.
I have a XMLRPC service that I call from the command line. It's using Zend framework.
the client looks like this:
$server = new Zend_XmlRpc_Client('http://hostname/path/to/xmlrpc.server.php');
The file is located:
/var/www/html/path/to/xmlrpc.server.php
I have it hard coded now but wanted to populate the 'path/to/' generically.
I've tried:
function url(){
$protocol = $_SERVER['HTTPS'] ? "https" : "http";
return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
echo url();
Note: None of the $_SERVER options returned what I needed
but executing this from the command line gives me:
http://
Also getcwd()
give me:
/var/www/html/path/to
How can I get:
http://path/to
Any thoughts?
The reason I would like this is if the project needs to change directories it should auto configure. Example:
If I move the project here:
/var/www/html/path/to/another/location
or here:
/var/www/public_html/path/to/another/location
or even here:
/path/to/document/root/path/to/another/location
I should get:
http://hostname/path/to/another/location
Thanks for any help
UPDATE:
I tried this but still not working as expected:
$hostname = `hostname`;
echo 'http://'.trim($hostname).'/'.basename(getcwd())."\n";
http://www.php.net/getcwd
Works perfectly when you're running a CLI app. Just make sure you know in which subdirectory you're running the application from and apply something with dirname() accordingly.
For example, if your docroot is set to: /var/www and your CLI app is inside /var/www/jobs/foobar/myapp.php, you could do something like this:
Method 1:
Method 2:
This might be sort of a hack job, but i just tried it and it worked nicely:
Like I said, I'm sure it's far from perfect; but it's a start!
Doesn't
$_SERVER['SCRIPT_FILENAME']
get you the path you need? As I understand it this should get you the absolute path when requested through the webserver, which I assume you're doing judging by the http url in your post. When called through the php command line it could be a relative path, which should be relative to cwd(). More info hereI always use these below and they work everywhere, either be called from CLI or any script or when being included under sub script(s):
Simple Answer: You can't.
As NikiC already stated in his comment to your question, invoking the script from the command line implies that there is no notion of a web server context available. The concept of a 'hostname' and a 'document root' only exist in the web server context, and are based on the configuration of the server. There is nothing in your
/var/www/something
directory that says 'Hey, I am a document root'.To illustrate, assume you have an Apache configured with two vhosts, using two document roots:
/var/www/top-docroot
/var/www/top-docroot/nested-docroot
.Your script is located at:
/var/www/top-docroot/nested-docroot/path/to/xmlrpc.server.php
What path should your script use, when invoked from the command line?
nested-docroot/path/to/xmlrpc.server.php
path/to/xmlrpc.server.php
While this is a pretty contrived example, it should still demonstrate the point that a document root can only be reliably determined in the context of a web server request, as it is read from configuration.
So when invoked from the command line, all your script could do would be to try to get the information from the web server configuration (which has no reliable place either), or to use some heuristics, such as assuming the common convention of document roots residing under
/var/www
. Both methods would be pretty unreliable, and you are probably better of sticking to hard coded values (or passing the information as a parameter on invocation).