<?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>Mark Foster's Blog &#187; PHP</title>
	<atom:link href="http://www.markfosteronline.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markfosteronline.com</link>
	<description>Misadventures in Technology</description>
	<lastBuildDate>Sat, 22 May 2010 05:18:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to use the PHP cURL module to make HTTP requests from PHP</title>
		<link>http://www.markfosteronline.com/2009/02/07/how-to-use-the-php-curl-module-to-make-http-requests-from-php/</link>
		<comments>http://www.markfosteronline.com/2009/02/07/how-to-use-the-php-curl-module-to-make-http-requests-from-php/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 18:55:00 +0000</pubDate>
		<dc:creator>mfoster</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[LinkedIn]]></category>

		<guid isPermaLink="false">http://www.markfosteronline.com/?p=489</guid>
		<description><![CDATA[Some of my previous posts talked about making HTTP/web requests from PHP with a focus on the PECL_HTTP request module: How to use the PECL HTTP (PECL_HTTP) Extension to make HTTP requests from PHP How to: PECL HTTP request exception and error handling PHP HttpRequest class options and notes How to use the file_get_contents() function [...]]]></description>
			<content:encoded><![CDATA[<p>Some of my previous posts talked about making HTTP/web requests from PHP with a focus on the PECL_HTTP request module:</p>
<ul>
<li><a href="http://www.markfosteronline.com/2009/01/04/how-to-use-the-pecl-http-pecl_http-extension-to-make-http-requests-from-php/">How to use the PECL HTTP (PECL_HTTP) Extension to make HTTP requests from PHP</a></li>
<li><a href="http://www.markfosteronline.com/2009/01/06/how-to-pecl-http-request-exception-and-error-handling/">How to: PECL HTTP request exception and error handling</a></li>
<li><a href="http://www.markfosteronline.com/2009/01/12/php-httprequest-class-options-and-notes/">PHP HttpRequest class options and notes</a></li>
<li><a href="http://www.markfosteronline.com/2009/01/18/how-to-use-the-file-get-contents-function-to-make-an-http-request-from-php/">How to use the file_get_contents() function to make an HTTP request from PHP</a></li>
</ul>
<p>The last post mentioned above covered using the built-in file_get_contents() as an alternative if you are unable to install the HTTP PECL extension or need minimal HTTP functionality.  This post will look at a third method of making HTTP requests from PHP, using the <a href="http://us2.php.net/manual/en/book.curl.php">PHP Client URL Library</a> AKA <a href="http://us2.php.net/manual/en/book.curl.php">cURL library</a>.  The PHP cURL library essentially uses the cURL library from the <a href="http://curl.haxx.se/">cURL command line utility</a> and makes the calls available via PHP functions.</p>
<p>Installing the PHP cURL library on Ubuntu requires just a couple simple steps:</p>
<ul>
<li>PHP needs to compile in the cURL library but if you are using Ubuntu you can simply execute the following shell command instead of doing a custom PHP build:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> php5-curl</pre></div></div>

</li>
<li>Restart Apache once the library has installed.</li>
<li>Call a page with the phpinfo() function and look for a &#8220;curl&#8221; section.  It should be listed there if everything installed correctly:
<p><img src="http://www.markfosteronline.com/wp-content/uploads/2009/02/curlinfo1.jpg" alt="curlinfo1" title="curlinfo1" width="453" height="91" class="alignnone size-full wp-image-640" />
</li>
</ul>
<p>Once the cURL library is added you can call the curl functions which are documented <a href="http://us2.php.net/manual/en/book.curl.php">here</a>.  The following simple example makes a call to www.example.com.  You will notice that I did not &#8220;echo&#8221; the return of curl_exec() to display it.  This is because by default, the curl_exec() function displays the result and returns a true on success, false on failure.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$curl_handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.example.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If you want to assign the output to a variable so you can do something with it, you will need to set the CURLOPT_RETURNTRANSFER option:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$curl_handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.example.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl_handle</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$results</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The PHP cURL library has a <a href="http://us2.php.net/manual/en/function.curl-setopt.php">variety of options you can set using the curl_setopt() function</a>.  This includes setting GET and POST requests, setting fields for each, etc.</p>
<p>That is the five minute version of the PHP cURL library.  Another quick way to make an HTTP request is to just make a system call to the &#8220;wget&#8221; command utility which is included on most *nix systems:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">system</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;wget -O - http://www.example.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This pretty cool but I think I prefer the other methods because they all run under the Apache process.  That&#8217;s it for this post!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markfosteronline.com/2009/02/07/how-to-use-the-php-curl-module-to-make-http-requests-from-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to setup and use the Xdebug Extension for PHP Profiling</title>
		<link>http://www.markfosteronline.com/2009/01/29/how-to-setup-and-use-the-xdebug-extension-for-php-profiling/</link>
		<comments>http://www.markfosteronline.com/2009/01/29/how-to-setup-and-use-the-xdebug-extension-for-php-profiling/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 06:02:22 +0000</pubDate>
		<dc:creator>mfoster</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[LinkedIn]]></category>

		<guid isPermaLink="false">http://www.markfosteronline.com/?p=291</guid>
		<description><![CDATA[A profiling tool can provide valuable information about bottlenecks in your code. I believe profiling is a critical aspect of optimizing because it will tell you about your real code bottlenecks as opposed to your perceived bottlenecks. This enables you to focus your resources on areas that will provide the most performance benefit for your [...]]]></description>
			<content:encoded><![CDATA[<p>A profiling tool can provide valuable information about bottlenecks in your code.  I believe profiling is a critical aspect of optimizing because it will tell you about your real code bottlenecks as opposed to your perceived bottlenecks.  This enables you to focus your resources on areas that will provide the most performance benefit for your effort.  Most programming languages and environments have one or more profiling tools available and PHP is no exception.</p>
<p><a href="http://www.xdebug.com">Xdebug</a> is a PHP extension that provides valuable debugging information such as stack traces, functions traces, profiling, code coverage analysis, etc.  There is another PHP tool called <a href="http://www.php-debugger.com/dbg/">DBG</a> that has similar functionality but this post will focus on using Xdebug.</p>
<h3>Setup Xdebug</h3>
<p>Xdebug is a PECL module and can be installed using the <a href="http://www.markfosteronline.com/2009/01/04/how-to-install-a-php-pecl-extensionmodule-on-ubuntu/">PECL installation instructions in one of my previous posts</a>.</p>
<ul>
<li>If you have already setup PEAR you just need to run the following from a shell:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> pecl <span style="color: #c20cb9; font-weight: bold;">install</span> xdebug</pre></div></div>

<p>If everything goes well Xdebug should download, build, and install.  You may get a message telling you:</p>
<div class="result">
You should add &quot;extension=xdebug.so&quot; to php.ini
</div>
<p>Go ahead and add the line. On an Ubuntu server you will probably find the php.ini file here: /etc/php5/apache2/php.ini</li>
<li>Restart Apache, or whatever web server you are using, so the change will take effect.<br />
If you are running Apache on Ubuntu it would be:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

</li>
<li>Write a phpinfo.php page with the following code and then point a browser at it.  It should show the Xdebug module there in addition to many other things:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">phpinfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</ul>
<p>At this point the Xdebug extension should be installed.  For more detailed instructions on a PECL extension install see my post: <a href="http://www.markfosteronline.com/2009/01/04/how-to-install-a-php-pecl-extensionmodule-on-ubuntu/">How to install a PHP PECL extension/module on Ubuntu</a>.  Note that there is a problem <a href="http://www.maxhorvath.com/2008/08/how-to-enable-the-xdebug-debugger-in-zend-studio-for-eclipse.html">running the Xdebug extension with Zend Studio</a> since it has it&#8217;s own debugger.</p>
<h3>Enable Xdebug profiling</h3>
<ul>
<li>By default, profiling is disable in Xdebug.  Enable it by adding the following entry to the php.ini file:

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">xdebug.profiler_enable<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">On</span></pre></div></div>

<p>On on a linux box there is often a php.d or conf.d folder that holds additional .ini file settings PHP will use.  On Ubuntu the path for this folder is &#8220;/etc/php5/apache2/conf.d&#8221;.  To prevent further cluttering my php.ini file with Xdebug settings, I created a xdebug.ini file to store all my Xdebug .ini file settings.  This file is located in the &#8220;/etc/php5/apache2/conf.d&#8221; so it is automatically scanned when Apache is restarted.</p>
<p>Now you might be tempted to enable the profiler in your script using the ini_set() function which, normally allows you to temporarily set a .ini setting for only the current script execution.  Unfortunately this does not work.  You must set it in the php.ini or a sub .ini file and restart the web server.</li>
<li>Restart your web server (I.e. Apache) once you have &#8220;xdebug.profiler_enable&#8221; set to &#8220;On&#8221;.</li>
</ul>
<p>By default, Xdebug will write a cachegrind.out file to the /tmp folder.  It will be named something like cachegrind.out.22373 where the number at the end is the ID of the process that was profiled.  If you are using Windows you will probably need to change the default folder.  Also by default, this file will be overwritten by each script execution so you don&#8217;t have to worry about it getting too big.  The output file behavior is highly customizable and a complete list of Xdebug settings can be found <a href="http://www.xdebug.org/docs/all_settings">here</a>.</p>
<h3>Call your script and display the analysis</h3>
<p>With Xdebug enabled, pull up the page in your browser that you want to profile.  If everything is working OK a cachegrind.out file should show up in the /tmp folder.</p>
<p>There are a couple programs you can use to open and analyze the cachegrind file:</p>
<ul>
<li><a href="http://sourceforge.net/projects/wincachegrind/">WinCacheGrind</a> (for Windows)</li>
<li><a href="http://kcachegrind.sourceforge.net/html/Home.html">KCacheGrind</a> (for Linux)</li>
</ul>
<h3>WinCacheGrind</h3>
<p>WinCacheGrind is not very featured but it will tell you the main thing you need to know, which is where your PHP application is spending its time.  Click on the screen shot below to see the full output of my test script:</p>
<p><a  href='http://www.markfosteronline.com/wp-content/gallery/cachegrind/wincachegrind.jpg' title=''><img src='http://www.markfosteronline.com/wp-content/gallery/cachegrind/thumbs/thumbs_wincachegrind.jpg' alt='wincachegrind screen shot' class='ngg-singlepic ngg-none' /></a></p>
<p>The script makes an external call to example.com using a file_get_contents() function.  Based on this analysis I might try caching the call results and only make the call at some interval to keep the cache updated.  This would eliminate almost 75% of the application&#8217;s overhead and is just one example of an easy-to-fix bottleneck that profiling will help identify.</p>
<h3>KCacheGrind</h3>
<p>KCacheGrind does essentially the same thing as WinCacheGrind but it is geared for the Linux desktop and has quite a few more bells and whistles:</p>
<p><a  href='http://www.markfosteronline.com/wp-content/gallery/cachegrind/kcachegrind1.jpg' title=''><img src='http://www.markfosteronline.com/wp-content/gallery/cachegrind/thumbs/thumbs_kcachegrind1.jpg' alt='kcachegrind screen shot' class='ngg-singlepic ngg-none' /></a></p>
<p>KCacheGrind includes a map feature that graphically represents the percentages of where the test application spent the time:</p>
<p><a  href='http://www.markfosteronline.com/wp-content/gallery/cachegrind/kcachegrind2.jpg' title=''><img src='http://www.markfosteronline.com/wp-content/gallery/cachegrind/thumbs/thumbs_kcachegrind2.jpg' alt='kcachegrind screen shot' class='ngg-singlepic ngg-none' /></a></p>
<p>KCacheGrind also includes a graph feature with an export option that displays a tree diagram of the linkage between all the includes and functions:</p>
<p><a  href='http://www.markfosteronline.com/wp-content/gallery/cachegrind/kcachegrindexport1.jpg' title=''><img src='http://www.markfosteronline.com/wp-content/gallery/cachegrind/thumbs/thumbs_kcachegrindexport1.jpg' alt='kcachegrindexport1.jpg' class='ngg-singlepic ngg-none' /></a></p>
<p>That&#8217;s it for this post.  Have fun profiling!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markfosteronline.com/2009/01/29/how-to-setup-and-use-the-xdebug-extension-for-php-profiling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use the file_get_contents() function to make an HTTP request from PHP</title>
		<link>http://www.markfosteronline.com/2009/01/18/how-to-use-the-file-get-contents-function-to-make-an-http-request-from-php/</link>
		<comments>http://www.markfosteronline.com/2009/01/18/how-to-use-the-file-get-contents-function-to-make-an-http-request-from-php/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 01:54:51 +0000</pubDate>
		<dc:creator>mfoster</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[LinkedIn]]></category>

		<guid isPermaLink="false">http://www.markfosteronline.com/?p=482</guid>
		<description><![CDATA[In a previous post I talked about using the HttpRequest object and functions in the PECL_HTTP extension to make HTTP requests from PHP. In some cases you may be limited to using functionality built into the PHP core. The file_get_contents() function has less features than the PECL_HTTP extension but it is built into PHP 4.3 [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.markfosteronline.com/2009/01/04/how-to-use-the-pecl-http-pecl_http-extension-to-make-http-requests-from-php/">previous post</a> I talked about using the <a href="http://us3.php.net/manual/en/class.httprequest.php">HttpRequest object</a> and <a href="http://us2.php.net/manual/en/ref.http.php">functions</a> in the PECL_HTTP extension to make HTTP requests from PHP.  In some cases you may be limited to using functionality built into the PHP core.  The <a href="http://us2.php.net/manual/en/function.file-get-contents.php">file_get_contents()</a> function has less features than the PECL_HTTP extension but it is built into PHP 4.3 and up.  Here is an example of using it to retrieve the landing page at www.example.com:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.example.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Someone hit that easy button.</p>
<p>The file_get_contents() functions as well as many other PHP file functions implement a streams abstraction layer largely conceived by <a href="http://netevil.org/">Mr. Wez Furlong</a>.  This abstraction layer is what enables many of the PHP file functions to access network resources.  Given this functionality &#8220;file&#8221; seems a misnomer.</p>
<p>The file_get_contents() function uses an HTTP GET request but what if you want to do a POST without using <a href="http://us2.php.net/manual/en/book.curl.php">cURL</a> or the <a href="http://pecl.php.net/package/pecl_http">PECL_HTTP extension</a>?  <a href="http://netevil.org/">Furlong</a> posted an <a href="http://netevil.org/blog/2006/nov/http-post-from-php-without-curl">article here</a> on how to do just that.</p>
<p>This next code example uses the file_get_contents() function again but this time a few options are set first using the stream_context_create() function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$http_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'http'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'user_agent'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Mark's Browser&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'max_redirects'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.example.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$http_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Note that the array passed to the stream_context_create() function can also be used to specify a POST method, which is how Furlong does so in his <a href="http://netevil.org/blog/2006/nov/http-post-from-php-without-curl">blog post</a>.</p>
<p>There is still yet another way to make an HTTP request from PHP that I haven&#8217;t covered yet using the <a href="http://us2.php.net/manual/en/book.curl.php">PHP built-in cURL functions</a>.  I will cover these in a separate blog post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markfosteronline.com/2009/01/18/how-to-use-the-file-get-contents-function-to-make-an-http-request-from-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to make your PHP application check for its dependencies</title>
		<link>http://www.markfosteronline.com/2009/01/18/how-to-make-your-php-application-check-for-its-dependencies/</link>
		<comments>http://www.markfosteronline.com/2009/01/18/how-to-make-your-php-application-check-for-its-dependencies/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 20:09:44 +0000</pubDate>
		<dc:creator>mfoster</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[LinkedIn]]></category>

		<guid isPermaLink="false">http://www.markfosteronline.com/?p=289</guid>
		<description><![CDATA[The very informative phpinfo() function The phpinfo() function displays just about everything you want to know about your PHP installation. It includes info on all your PECL and PEAR modules so it is a quick way to check what&#8217;s installed. It will also tell you useful web server information including any query strings you pass. [...]]]></description>
			<content:encoded><![CDATA[<h3>The very informative phpinfo() function</h3>
<p>The <a href="http://us2.php.net/manual/en/function.phpinfo.php">phpinfo()</a> function displays just about everything you want to know about your PHP installation.  It includes info on all your PECL and PEAR modules so it is a quick way to check what&#8217;s installed.  It will also tell you useful web server information including any query strings you pass.  To display all this good info just point your browser at a page on your server that contains the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">phpinfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That&#8217;s it!</p>
<h3>Automatic dependency checking</h3>
<p>The phpinfo() function will give us a page that displays a lot of info.  You can pass it bitwise constants to narrow down the information displayed but what if we want to check for specific items?</p>
<p>In my humble opinion, when developing software or anything in general, it is a good idea to design things so that the end user will not even need a manual because the user interface is obvious.  When something doesn&#8217;t work, it should be equally obvious how to fix it.</p>
<p>If you write a PHP application that others will install and use, it is a good idea to check for dependencies when they try to use the application.  This way even if they don&#8217;t read your documentation they will quickly know why the software is not working.</p>
<h3>Using phpversion(), PHP_VERSION, and version_compare() to check the PHP verson</h3>
<p>To get the core PHP version you can use either of the following methods:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;or&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">PHP_VERSION</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The above code should output something like this:</p>
<div class="result">
5.2.6-2ubuntu4<br />
or<br />
5.2.6-2ubuntu4
</div>
<p>If you are using Ubunto or some other distribution, you will note that some additional stuff is tack on to the version number (I.e. &#8220;-2ubuntu4&#8243;).  This makes a comparison to your expected version a little tricky but you can use a substr()/strpos() combo to get what you need.  There is an easier way to do the comparison though.  The version_compare() function is &#8220;PHP-standardized&#8221; version aware.  So we can do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">version_compare</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">PHP_VERSION</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'5.0.0'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You are using PHP version '</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">PHP_VERSION</span> <span style="color: #339933;">.</span> 
      <span style="color: #0000ff;">'This program requires PHP version 5.0.0 or higher.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You are using PHP 5.0.0 or higher. You are all set!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now you can check the PHP version and notify the user if it is not the minimum required version.</p>
<p>The PHP function documentation for each function at <a href="http://www.php.net">www.php.net</a> include the PHP versions that contain the function in the upper left hand corner:</p>
<p><img src="http://www.markfosteronline.com/wp-content/uploads/2009/01/func_version.gif" alt="substr function version on php.net" title="substr function version on php.net" width="497" height="287" class="alignnone size-full wp-image-426" /></p>
<p>You can use this to learn what versions of PHP include the functions you are using in your code to help identify your minimum PHP version requirement.</p>
<h3>Using get_loaded_extensions() to check for extensions</h3>
<p>The get_loaded_extensions() function will return an array of PHP extensions that you can use to check if a specific extension is installed.  Use it in combination with with the in_array() function to check if the extension you require is loaded.  In this example I check if the <a href="http://pecl.php.net/package/pecl_http">PECL_HTTP</a> module is installed:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">get_loaded_extensions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The PECL_HTTP module is installed. '</span><span style="color: #339933;">.</span>
      <span style="color: #0000ff;">'You are all set!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The PECL_HTTP module is not installed. '</span><span style="color: #339933;">.</span>
      <span style="color: #0000ff;">'Please install it.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You can use the phpversion() function to check if extension is listed and if so, its version.  This code example not only checks if the PECL_HTTP module is installed, but also checks it&#8217;s version:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The PECL_HTTP module is not installed. '</span><span style="color: #339933;">.</span>
      <span style="color: #0000ff;">'Please download it from '</span><span style="color: #339933;">.</span>
      <span style="color: #0000ff;">'&lt;a href=&quot;http://pecl.php.net/package/pecl_http&quot;&gt;here&lt;/a&gt; '</span><span style="color: #339933;">.</span>
      <span style="color: #0000ff;">' and install it.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">version_compare</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'1.6.0'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&gt;='</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The PECL_HTTP extension is installed and '</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'version 1.6.0 or higher. You are all set!'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Please upgrade your PECL_HTTP extension to '</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'version 1.6.0 or higher. You can download it '</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;a href=&quot;http://pecl.php.net/package/pecl_http&quot;&gt;here'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>Use function_exists() to check for individual functions</h3>
<p>So far the methods for checking dependencies have been somewhat broad.  They check that the script has a certain version of PHP or extensions installed and that will likely be good enough in most cases.  If you really want to be thorough you can also check if specific functions are available using the function_exists() method.  In this example I check that the http_request() module, which is part of the PECL_HTTP extension, is there before I use it.  If it is not, I use the less featured, built in, file_get_contents() function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http_get&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Using the http_get():&lt;br/&gt;'</span> <span style="color: #339933;">.</span>
        <span style="color: #990000;">http_parse_message</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">http_get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.example.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Using the file_get_contents():&lt;br/&gt;'</span> <span style="color: #339933;">.</span> 
        <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.example.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>Check for include files</h3>
<p>Here is a simple way to check for include files.  It doesn&#8217;t verify their content but you can at least make sure they are there:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'httptest.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The httptest.php include file is missing.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'httptest.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>Wrap up</h3>
<p>Checking dependencies is an important part of building robust software and hopefully the above techniques will help accomplish that.  Even if your end user is a very technical they will likely appreciate a good dependency checking mechanism that quickly tells them whats missing to save them time.  If your software will be used by non-technical users you might want to automatically and gracefully downgrade your software feature set instead of generating errors and asking them for something they won&#8217;t know how to do.  Usability is king!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markfosteronline.com/2009/01/18/how-to-make-your-php-application-check-for-its-dependencies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP HttpRequest class options and notes</title>
		<link>http://www.markfosteronline.com/2009/01/12/php-httprequest-class-options-and-notes/</link>
		<comments>http://www.markfosteronline.com/2009/01/12/php-httprequest-class-options-and-notes/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 05:14:05 +0000</pubDate>
		<dc:creator>mfoster</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[LinkedIn]]></category>

		<guid isPermaLink="false">http://www.markfosteronline.com/?p=287</guid>
		<description><![CDATA[In a recent post I talked about the PECL_HTTP extension for PHP. In this post I will cover a few of the options you can set for the HttpRequest object and related functions. The PECL_HTTP extension allows you to set a number of options when you make a request. Usually you put the options in [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.markfosteronline.com/2009/01/04/how-to-use-the-pecl-http-pecl_http-extension-to-make-http-requests-from-php/">recent post</a> I talked about the PECL_HTTP extension for PHP.  In this post I will cover a few of the options you can set for the HttpRequest object and related functions.</p>
<p>The <a href="http://pecl.php.net/package/pecl_http">PECL_HTTP</a> extension allows you to set a number of options when you make a request.  Usually you put the options in a key=>value array and pass the array as an argument to the request functions (I.e. <a href="http://us.php.net/manual/en/function.http-get.php">http_get()</a>, <a href="http://us.php.net/manual/en/function.http-post-data.php">http_post_data()</a>, <a href="http://us.php.net/manual/en/function.http-request.php">http_request()</a>, etc.) or assign the array to the HttpRequest object using the <a href="http://us.php.net/manual/en/function.httprequest-setoptions.php">setOptions()</a> method.  Here is a code example using the HttpRequest object:</p>
<div class="code">
$http_req = new HttpRequest(&quot;http://www.example.com&quot;);<br />
$http_req->setOptions(array(timeout=>10,useragent=>&quot;MyScript&quot;));
</div>
<p>In the above code the timeout was set to 10 seconds and the user agent, which is a request header that identifies the browser to the server, is set to &#8220;MyScript&#8221;. I am going to cover just a few of the options but a full list of all the <a href="http://us.php.net/manual/en/http.request.options.php">request options</a> can be found <a href="http://us.php.net/manual/en/http.request.options.php">here</a>.</p>
<h3>timeout</h3>
<p>The timeout option specifies the maximum amount of time in seconds that the request may take to complete.  Set this too high and your HTTPD process that PHP is running in could be stalled for quite a bit waiting for a request that may never complete.  If you set it too low you might have problems with sites that are just slow to respond.  This may require some tweaking depending on what you are doing.  If you are making requests to a <a href="http://www.gws.com.tw/">web server in Taiwan</a> you might want to set the timeout a bit higher.  The default timeout does not appear to be documented in the <a href="http://us.php.net/manual/en/http.request.options.php">HttpRequest options page on PHP.NET</a> (that I could find) but if you look at the http_request_api.c file in the <a href="http://cvs.php.net/viewvc.cgi/pecl/http/">HTTP_PECL source code</a>, it looks like it is 0L AKA nothing:</p>
<div class="code">
HTTP_CURL_OPT(CURLOPT_TIMEOUT, 0L);
</div>
<p>This indicates it will wait forever unless you explicitly set a timeout so it might be a good idea to set one!  I put together a page that makes an HTTP request and then another one that will sleep for some number of seconds that I can test against.</p>
<p>Here is the code for the page that will sleep:</p>
<div class="code">
&lt;?php</p>
<p>echo &quot;Sleeping.&quot;;<br />
sleep(30);</p>
<p>?>
</p></div>
<p>Here is the code for the page that will make the HTTP request:</p>
<pre>
&lt;?php
$http_req = new HttpRequest(&quot;http://localhost/alongtime.php&quot;);
$http_req->setOptions(array(timeout=>10, useragent=>&quot;MyScript&quot;));
try {
    $http_req->send();
} catch (HttpException $ex) {
    if (isset($ex->innerException)){
        echo $ex->innerException->getMessage();
        exit;
    } else {
        echo $ex;
        exit;
    }
} //end try block
echo $http_req->getResponseBody();
?>
</pre>
<p>When I pull up the page that makes the HTTP request in my browser I get the following error:</p>
<div class="error">
Timeout was reached; Operation timed out after 10000 milliseconds with 0 bytes received (http://localhost/alongtime.php)
</div>
<p>If I don&#8217;t set the timeout option at all, the page responds 30 seconds later since it will wait forever or at least the 30 second sleep time on the target page.</p>
<h3>connecttimeout</h3>
<p>The connecttimeout option indicates the maximum amount of time in seconds that the request may take just connecting to the server.  This does not include the time it takes for the server to process and return the data for the request.  This option will have the same considerations as above although the number should be considerably lower since it is only the connection timeout and not the timeout for the whole request.  Again, the default value is not documented but if you look at the http_request_api.c file in the <a href="http://cvs.php.net/viewvc.cgi/pecl/http/">HTTP_PECL source code</a>, it looks like it is 3 seconds:</p>
<div class="code">
HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, 3);
</div>
<h3>dns_cache_timeout</h3>
<p>One of the interesting features of the HTTP_PECL extension is that it will cache DNS lookups.  Some of the Windows operating systems do this but many of the Linux distributions do not by default.  By the way, if you want to clear your cached DNS lookup entries on a Windows box use the command &#8220;ipconfig /flushdns&#8221;.  If you are making multiple requests to the same site, DNS lookup caching should provide a significant performance advantage because a round trip to the DNS server isn&#8217;t required for every request.  The dns_cache_timeout option sets the number of seconds that will pass before the cached DNS lookup results will expire and a new DNS lookup will be performed.  Again, the default value is not documented but if you look at the http_request_api.c file in the <a href="http://cvs.php.net/viewvc.cgi/pecl/http/">HTTP_PECL source code</a>, it looks like it is 60 seconds which is probably fine for most applications:</p>
<div class="code">
HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT, 60L);
</div>
<h3>redirect</h3>
<p>The redirect option determines how many redirects the request will follow before it returns with a response.  The default is 0 (this IS documented), which may not work in many situations because some applications respond with one or two redirects for authentication, etc.  If you set this too high your application may get bounced around too many times and never return.  I have not tried it but you could probably put someone in a redirect loop.  Anyway, a value of around 4 or 5 should be adequate for most applications I would imagine.</p>
<h3>useragent</h3>
<p>The useragent option allows you to specify a different User-Agent request header to send to the server than the default which is &#8220;PECL::HTTP/x.y.z (PHP/x.y.z)&#8221; where x.y.z are the versions.</p>
<p>I made a little one-liner test page that returns the user agent info sent to the server:</p>
<div class="code">
&lt;?php</p>
<p>echo $_SERVER['HTTP_USER_AGENT'];</p>
<p>?>
</p></div>
<p>If I make an HTTP request to this page using the HttpRequest object without setting the useragent I get:</p>
<div class="result">
PECL::HTTP/1.6.2 (PHP/5.2.6-2ubuntu4)
</div>
<p>If I do something like this:</p>
<div class="code">
$http_req->setOptions(array(timeout=>10, useragent=>&quot;Mark&#8217;s Browser&quot;));
</div>
<p>I will get:</p>
<div class="result">
Mark&#8217;s Browser
</div>
<p>The reason I bring this up is because some applications that you might make a request to may respond different depending on your user agent setting.  In some cases you may need to spoof a specific browser to get what you are after.</p>
<h3>Conclusion</h3>
<p>As mentioned before, there are many more <a href="http://us.php.net/manual/en/http.request.options.php">HttpRequest options</a>.  I just covered a few notable ones that I have some limited experience with.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markfosteronline.com/2009/01/12/php-httprequest-class-options-and-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
