<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Blog of Angelo &#187; php-cli</title>
	<atom:link href="http://angelo.mandato.com/tag/php-cli/feed/" rel="self" type="application/rss+xml" />
	<link>http://angelo.mandato.com</link>
	<description>where logic becomes print</description>
	<lastBuildDate>Mon, 31 Oct 2011 16:12:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Integrating PHP Command Line Scripts with Existing Web Projects</title>
		<link>http://angelo.mandato.com/2009/01/06/integrating-php-command-line-scripts-with-existing-web-projects/</link>
		<comments>http://angelo.mandato.com/2009/01/06/integrating-php-command-line-scripts-with-existing-web-projects/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 15:17:14 +0000</pubDate>
		<dc:creator>Angelo</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[php-cli]]></category>
		<category><![CDATA[verbose]]></category>

		<guid isPermaLink="false">http://www.compiledweekly.com/?p=116</guid>
		<description><![CDATA[After reading the post on Johan Mares site about the PHP command line interface, I thought I would indulge in the details how I&#8217;ve been using the PHP cli for some of my web based applications. First, some of my web apps have multiple configuration files which are determined by the $_SERVER['HTTP_HOST'] value. If (stricmp( [...]]]></description>
			<content:encoded><![CDATA[<p>After reading the post on <a title="Johan Mares" href="http://blog.johan-mares.be/ict/php/running-php-shell-scripts/" target="_blank">Johan Mares</a> site about the PHP command line interface, I thought I would indulge in the details how I&#8217;ve been using the PHP cli for some of my web based applications.</p>
<p>First, some of my web apps have multiple configuration files which are determined by the $_SERVER['HTTP_HOST'] value. If (stricmp( $_SERVER['HTTP_HOST'],&#8217;compiledweekly&#8217;) ) { // then I load compiledweekly config file }. So with that in mind, I had to add to the top of my cli scripts the following line:</p>
<p style="padding-left: 30px;">$_SERVER['HTTP_HOST'] = &#8216;compiledweekly.com&#8217;;</p>
<p>This required me to have to edit the command line script every time i used it with another site. Here&#8217;s the trick: pass arguments to your command line so your script can parse them. Here&#8217;s how I did it to also include a verbose mode.</p>
<pre>	if( count($argv) > 1 )
	{
		for( $x = 1; $x < count($argv); $x++ )
		{
			switch($argv[$x])
			{
				case '--verbose': { // Print results to std out
					$Verbose = true;
				}; break;
				case '--host': { // Print results to std out
					$_SERVER['HTTP_HOST'] = trim($argv[$x+1]);
				}; break;
			}
		}
	}

	if( $Verbose ) echo "Starting script...nn";
	// Continue with your script below</pre>
<p>So with the following example I can run my script for my compiledweekly.com site with verbose information. Example:</p>
<p style="padding-left: 30px;">/path/to/script.php --verbose --host compiledweekly.com</p>
<p>Now if you use your php script in a cron task, don't include the --verbose and make sure you check the $Verbose flag before printing any results. Don't forget to add to the end of the command line " &gt; /dev/null 2&gt;&amp;1" minus the quotes, this sends any std out and std error messages to a <a title="What does &gt; /dev/null 2&gt;&amp;1 mean" href="http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/" target="_blank">black hole</a>.<em><br />
</em></p>
<p>If your command line script is saved in a web accessible folder, here's a line you should add to the top so no web browser can execute your script:</p>
<p style="padding-left: 30px;">if( php_sapi_name() != 'cli' ) die('Access denied.');</p>
<p>This will take your command line apps to a new level while giving you the ability to use existing web code.</pre>
]]></content:encoded>
			<wfw:commentRss>http://angelo.mandato.com/2009/01/06/integrating-php-command-line-scripts-with-existing-web-projects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

