<?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>Christian Schenk&#187; php</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ideas about clean if statements</title>
		<link>http://www.christianschenk.org/blog/ideas-clean-if-statements/</link>
		<comments>http://www.christianschenk.org/blog/ideas-clean-if-statements/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 03:40:51 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[clean]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[statements]]></category>
		<category><![CDATA[Thoughts]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=59</guid>
		<description><![CDATA[If you&#8217;re reading a lot of code you may get to the point where you&#8217;d like it to be clean, so it&#8217;s easier for you to read. There&#8217;re a lot of resources about the beauty of code around and in this post I&#8217;d like to share some ideas about writing concise if statements.
Although these ideas [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re reading a lot of code you may get to the point where you&#8217;d like it to be clean, so it&#8217;s easier for you to read. There&#8217;re a lot of resources about the beauty of code around and in this post I&#8217;d like to share some ideas about writing concise <code>if</code> statements.</p>
<p>Although these ideas may apply to a lot of different programming languages I chose to give examples written in Java, PHP or Python. There should be no problem to translate this into other, similar languages.</p>
<p><span id="more-59"></span></p>
<h2>Indentation hell</h2>
<p>A lot of years ago I read a text file describing the preferred coding style for the Linux kernel: it said that you&#8217;re screwed anyway if you need more than three levels of indentation. This stems from hackers using a screen resolution that allows a certain number of characters only. Writing code that uses a lot of nested statements causes the problem that you can&#8217;t read everything on a 80&#215;25 terminal; wrapping lines may make things worse.</p>
<p>Back then the solution was to either increase the screen&#8217;s resolution <em>or</em> write clean code. Even today the latter is a good idea not only when writing code but maybe configuration files in general. Imagine a sysadmin in the server room with an old monitor only &#8211; he&#8217;ll thank you that you formatted the comments with a text width of 80 characters or less.</p>
<p>Let&#8217;s have a look at an example like the following. You&#8217;ll notice that I&#8217;ve use four levels of indentation: a for loop, an if statement, another for loop and another if statement.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> x<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> z<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">==</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// do something special</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can spot this pattern by looking at it&#8217;s tail: a lot of closing curly brackets. Refactoring this snippet consists of splitting the code into various functions and calling them at the right time. One solution might look like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> a<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> x<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> y<span style="color: #009900;">&#41;</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> z<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">==</span> a<span style="color: #009900;">&#41;</span> do_something_special<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> do_something_special<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// do something special</span>
  <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, the code is much easier to read and unit testing it will be more fun too. Don&#8217;t be afraid of adding <em>extra</em> functions into your program when you could write it all into one method/function. The latter is harder to maintain and rest assured that more method/function calls won&#8217;t add a serious performance penalty to your application in any way.</p>
<h2>Inverting</h2>
<p>It&#8217;s likely that you&#8217;ve seen a method that starts with an if statement, almost all the method&#8217;s code is inside that statement and at the end of the method you find the else block with only one statement in it, maybe some error handling code.</p>
<p>Instead of writing if statements that span multiple pages and contain lots and lots of code the following technique allows you to have the if statement on one line. As discussed above this avoids an unnecessary level of indentation as well.</p>
<p>Let&#8217;s have a look at an example: a simple method containing an if statement with another if statement which holds the real code of the method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> doIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something_else <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;"># do something...
</span>      <span style="color: #666666; font-style: italic;"># ...
</span>    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Simply by inverting the if statements we can write them on one line, producing the following, much cleaner code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> doIt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>something_else <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;"># do something...
</span>  <span style="color: #666666; font-style: italic;"># ...
</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, not just the statement but the whole method is easier to read. If you&#8217;re checking preconditions, this is the way to go. This also prevents the indentation hell mentioned above.</p>
<h2>Splitting</h2>
<p>Speaking of checks for the preconditions of certain methods, one might tend to put all checks into one single if statement. This may save you from e.g. writing code to throw an exception more than once but may result in a hard to understand if statement.</p>
<p>I recommend writing if statements that test only one or two conditions. Have a look at the following code which contains a giant if statement.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span>a, b, c<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">10</span> <span style="color: #ff7700;font-weight:bold;">or</span> a <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">20</span> <span style="color: #ff7700;font-weight:bold;">or</span> \
     b == <span style="color: #008000;">False</span> <span style="color: #ff7700;font-weight:bold;">or</span> \
     c == <span style="color: #483d8b;">'unknown'</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> SomeException<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Invalid arguments'</span><span style="color: black;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># do something...</span>
  <span style="color: #808080; font-style: italic;"># ...</span></pre></div></div>

<p>We could argue that the code is pretty clean and easy to understand. True in this case, but the semantics aren&#8217;t obvious. What about introducing two more methods and raising more specific exceptions? Would result in this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span>a, b, c<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> inRange<span style="color: black;">&#40;</span>a, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> SomeException<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Argument a is out of bounds'</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> isValid<span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> isValid<span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> SomeException<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Argument b or c are invalid'</span><span style="color: black;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># do something...</span>
  <span style="color: #808080; font-style: italic;"># ...</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> inRange<span style="color: black;">&#40;</span>a, start, end<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">return</span> a <span style="color: #66cc66;">&gt;</span>= start <span style="color: #ff7700;font-weight:bold;">and</span> a <span style="color: #66cc66;">&lt;</span>= end
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> isValid<span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: black;">&#40;</span> a == <span style="color: #008000;">False</span> <span style="color: #ff7700;font-weight:bold;">or</span> a == <span style="color: #483d8b;">'unknown'</span> <span style="color: black;">&#41;</span></pre></div></div>

<p>This may bloat your code a little bit but helps you reading the code almost like a sentence. Splitting one large if statement into smaller ones may result in sensible error messages too. And the introduction of small helper methods makes unit testing fun again.</p>
<h2>Sorting</h2>
<p>If you&#8217;re using the previous technique &#8211; splitting a single if statement into smaller, more concise portions &#8211; you should consider the order of the if statements. Let&#8217;s think about a simple example: in a <a href="/blog/friday-13th-joda-time/">recent post</a> I tried finding occurrences of Friday the 13th dates by iterating over a given time span. The method looked like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/* ... */</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    date <span style="color: #339933;">=</span> date.<span style="color: #006633;">plusDays</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>date.<span style="color: #006633;">getDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>date.<span style="color: #006633;">getDayOfWeek</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> DateTimeConstants.<span style="color: #006633;">FRIDAY</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The two if statements test whether the current day is a Friday the 13th date. First checking whether the current day is the 13th day of a month speeds things up a little bit because we&#8217;ll only check whether this day happens to be a friday &#8211; the other way around would be more expensive in terms of performance.</p>
<p>This technique also applies when your if statements call <em>expensive</em> methods to test a certain condition. You may want to call the ones with the least overhead first thus reducing the time spent in your if statements.</p>
<h2>Conclusion</h2>
<p>In this post I presented some simple solutions that should help you writing cleaner, better code regarding if statements. Following these rules will make your code much easier to read and to maintain. Just give it a try and you&#8217;ll definitely notice the positive difference.</p>
<p>Sure, I haven&#8217;t given any references to real projects helping you to verify that the examples given in this post exist in real projects. I think searching in real projects for code like the above shouldn&#8217;t be much of a problem. And I&#8217;m pretty sure you&#8217;ll agree on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/ideas-clean-if-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a simple proxy checker tool</title>
		<link>http://www.christianschenk.org/blog/building-simple-proxy-checker-tool/</link>
		<comments>http://www.christianschenk.org/blog/building-simple-proxy-checker-tool/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 06:45:41 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[checker]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=713</guid>
		<description><![CDATA[In case you&#8217;re trying to change the configuration of your proxy server you might want to check what appears in the logs of a web server returning data to your proxy. Maybe you want to suppress certain HTTP header fields or make sure that the browser&#8217;s user agent is forged correctly. 
This post discusses a [...]]]></description>
			<content:encoded><![CDATA[<p>In case you&#8217;re trying to change the configuration of your proxy server you might want to check what appears in the logs of a web server returning data to your proxy. Maybe you want to suppress certain HTTP header fields or make sure that the browser&#8217;s user agent is forged correctly. </p>
<p>This post discusses a PHP script which tries to display helpful information from the web server&#8217;s perspective. Although it may not be complete you can easily extend it to suit your needs.</p>
<p><span id="more-713"></span></p>
<h2>The tool</h2>
<p>All we need to do is to upload <a href="http://data.christianschenk.org/building-simple-proxy-checker-tool/proxy-tool.php.zip">this</a> script to a web server and access it through our proxy. The output shows us the information a web server may store in his log files, i.e. things like our IP address, the referrer, the browser&#8217;s user agent and proxy related fields like <code>Via</code> or <code>Forwarded-For</code>.</p>
<p>The code needed to implement this in PHP is pretty easy: just have a look at <code>$_SERVER</code> and you&#8217;ll find a lot of interesting information. In case you&#8217;re missing something have a look at PHP&#8217;s <a href="http://www.php.net/manual/en/reserved.variables.php">predefined variables</a>. Maybe you want to play around with cookies, check out <code>$_COOKIE</code> if you would like to display information about cookies.</p>
<p>Why do we need another proxy checker, there are plenty of websites doing this kind of stuff, you may ask. I think that depends on two factors: speed and reliability. Obviously, uploading the script to your web server at home and sending requests to this machine will be a lot faster than any web server on the internet. Second, you need reliable information, i.e. again it&#8217;s easier to trust your own web server and PHP script than some site on the net.</p>
<h2>Conclusion</h2>
<p>Using PHP it&#8217;s easy to bootstrap your own proxy checker tool. Although the script presented in this posts is really simple, it helps us to get the relevant information quick. Repeatedly requesting the script and tweaking the configuration of your proxy should help you to get the desired results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/building-simple-proxy-checker-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Using cURL if allow_url_fopen is not enabled</title>
		<link>http://www.christianschenk.org/blog/php-curl-allow-url-fopen/</link>
		<comments>http://www.christianschenk.org/blog/php-curl-allow-url-fopen/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 05:15:52 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[allow_url_fopen]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=711</guid>
		<description><![CDATA[In case you&#8217;re using PHP to retrieve data from a certain server you probably came across the problem that it may work for you but a client complained about lots of errors. It&#8217;s pretty likely that you&#8217;ve relied on the fact that allow_url_fopen is set to true. This way you can put pretty much anything [...]]]></description>
			<content:encoded><![CDATA[<p>In case you&#8217;re using PHP to retrieve data from a certain server you probably came across the problem that it may work for you but a client complained about lots of errors. It&#8217;s pretty likely that you&#8217;ve relied on the fact that <code>allow_url_fopen</code> is set to true. This way you can put pretty much anything &#8211; local path or a URL &#8211; into function calls like <code>include</code> or maybe <code>simplexml_load_file</code>.</p>
<p><span id="more-711"></span></p>
<p>If you&#8217;d like to get around this problem you can advice your client to make the necessary changes in his <code>php.ini</code> file. Most of the time this isn&#8217;t an option because the hosting company decided to disable this feature for security reasons. Since almost everybody got cURL installed we can use this to retrieve data from another web server.</p>
<h2>Implementation</h2>
<p>I&#8217;ll present a wrapper that helps you loading an XML file. It uses <code>simplexml_load_file</code> if <code>allow_url_fopen</code> is enabled. If this feature is disabled it employs <code>simplexml_load_string</code> and cURL. If none of this works we&#8217;ll throw an exception because we weren&#8217;t able to load the data.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> XMLWrapper <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadXML<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">ini_get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'allow_url_fopen'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load_fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</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: #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;">'curl_init'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load_curl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</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: #666666; font-style: italic;">// Enable 'allow_url_fopen' or install cURL.</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Can't load data.&quot;</span><span style="color: #009900;">&#41;</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;">private</span> <span style="color: #000000; font-weight: bold;">function</span> load_fopen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</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;">private</span> <span style="color: #000000; font-weight: bold;">function</span> load_curl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$curl</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</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</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</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</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The code is pretty simple, create an instance of the given class and call the <code>loadXML</code> method. It&#8217;ll call the <em>right</em> private method which finally loads the XML. Loading some XML is just an example, you can use this technique with e.g. <code>include</code> or <code>require</code> too.</p>
<h2>Conclusion</h2>
<p>In case <code>allow_url_fopen</code> isn&#8217;t enabled it&#8217;s easy to write code that works around this problem. This way clients tied to a hosting company with tight security constraints will be able to use your software. If the aforementioned solution doesn&#8217;t work because cURL isn&#8217;t installed either you can still decide to package the data &#8211; that would otherwise be downloaded &#8211; along with your module if that&#8217;s possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/php-curl-allow-url-fopen/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
