<?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; curl</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/curl/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>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>
