Welcome to thatlinuxbox.com Thursday, April 25 2024 @ 04:33 AM UTC

Specify QUERY_STRING to PHP CLI

  • Sunday, December 02 2012 @ 03:25 PM UTC
  • Contributed by:
  • Views: 10,481
Linux, Open Source, and Tech Stuff

Share
  • Google Plus
  • Facebook
  • Twitter
  • Reddit
  • LinkedIn
  • Digg

Thanks to an ancient http://PHP.net comment (http://www.php.net/manual/en/features....php#26201) and a Joomla! forum post (http://forum.joomla.org/viewtopic.php...3#p1318363), I was able to figure out how to run a Joomla php script from the command line and
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.
Specify QUERY_STRING to PHP CLI | 0 comments | Create New Account

The following comments are owned by whomever posted them. This site is not responsible for what they say.


User Functions

Login

Connect

thatlinuxbox.com is the home of Dan Stoner's Personal Blog, Photos, and More (opinions, rants, techno-babble, and possibly a few useful tidbits of knowledge).

Questions or Comments about this site? Contact danstoner _ at _ gmail.com.

RSS Feed for this blog

Other places to find me on the web:

Twitter

LinkedIn

GitHub

Support This Site

If you like something that you find on this site, please consider making a purchase through one of the links below or sending me an item from my Amazon Wish List.


The Clymb


Awesome VPS hosting by Linode.com