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";
By default, the Apache web root or Document root folder location is at /var/www/html.
This file is typically found under the apache-install/conf directory.
The DocumentRoot is the top-level directory in the document tree visible from the web and this directive sets the directory in the configuration from which Apache2 or HTTPD looks for and serves web files from the requested URL to the document root. For example: DocumentRoot "/var/www/html"
This line sets the Apache document root for the website to the directory. The block of code between the Directory tags simply enables directory listings in Apache and is safe to ignore if you are not following this tutorial verbatim.
To get the Apache DocumentRoot directory on Debian, Ubuntu Linux and it’s derivatives such as Linux Mint, run the following grep command. On CentOS, RHEL and Fedora Linux distributions, run the following command.
A. Apache has DocumentRoot directive. This directive sets the directory from which Apache will serve files. Unless matched by a directive like Alias, the server appends the path from the requested URL to the document root to make the path to the document. Example: DocumentRoot /home/www/theos.in.
for those who are unaware, on command line, there is a blank answer for document root because it is not defined. $_SERVER contains headers which won't be available in the CLI. The web server defines the document root. In the CLI, you aren't using a web server, so there is no document root.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With