Welcome to thatlinuxbox.com Thursday, November 21 2024 @ 11:06 AM UTC
Specify QUERY_STRING to PHP CLI
- Sunday, December 02 2012 @ 03:25 PM UTC
- Contributed by: Dan Stoner
- Views: 11,213
specify query parameters *without* having to modify any of the PHP code to parse the arguments / options. Most of the examples on the net suggest modifying the PHP script to parse the argv options. I also did not have luck using environment variables to specify QUERY_STRING.
For example, take a Joomla! article with a URL something like this:
mysite/index.php?option=com_content&id=10
Many people running Joomla have SEO-friendly URLs enabled, but the above is the simple (simplest?) query string to get to a particular article using the article id.
Now we can test a particular article at this site from the command line.
Change directory to the appropriate document root and run php as a command-line program:
$ php index.php '&option=com_content&id=10'
The magic sauce is that the first character of the query argument list is just another ampersand rather than the traditional question mark.
Why would I want to do this at all? As one of the Linux system administrators responsible for helping our University customers with their hosted web sites, I sometimes have to debug unusual issues. Having system administrators modify production customer code is not standard practice and unfortunately some of our web hosting customers are not saavy enough to debug unusual issues themselves. One of the tools in our sysadmin toolbox is strace. If I can get the script to repeat the behavior in the command line while strace is running I can often determine the cause of the issue. Running the script in the cli is frequently much easier than trying to catch the issue with strace on a live production site, in shared web infrastructure, etc. We can see file accesses failing, network requests to external servers that don't allow the php page to render until after the remote content is fetched, slow performance from a mysql database... all via the output of strace.
The following comments are owned by whomever posted them. This site is not responsible for what they say.