<?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>Monday By Noon &#187; cURL</title>
	<atom:link href="http://mondaybynoon.com/tag/curl/feed/" rel="self" type="application/rss+xml" />
	<link>http://mondaybynoon.com</link>
	<description>A resource for Web designers and developers to read about and discuss their craft.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:49:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>&#8226; Avoiding IFRAMES via PHP and cURL</title>
		<link>http://mondaybynoon.com/feeder/?FeederAction=clicked&#038;feed=Posts+%28RSS2%29&#038;seed=http%3A%2F%2Fmondaybynoon.com%2F20091123%2Favoiding-iframes-via-php-and-curl%2F&#038;seed_title=%26%238226%3B+Avoiding+IFRAMES+via+PHP+and+cURL</link>
		<comments>http://mondaybynoon.com/20091123/avoiding-iframes-via-php-and-curl/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 20:01:29 +0000</pubDate>
		<dc:creator>Jonathan Christopher</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Workbench]]></category>
		<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[IFRAME]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://mondaybynoon.com/?p=632</guid>
		<description><![CDATA[With IFRAMES being so outdated, there is an alternative using PHP and cURL<br /><p><a href='http://rss.buysellads.com/click.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=632&c=2129654455' target='_blank' rel='nofollow'>
				<img src='http://rss.buysellads.com/img.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=632&c=2129654455' border='0' alt='' /></a></p><p><a href='http://buysellads.com/buy/sitedetails/pubkey/2ee344414ac81fbb0f9de6ab08e9831e/zone/1269068' target='_blank'>Advertise here with BSA</a></p>]]></description>
			<content:encoded><![CDATA[<p>A current project requires integration with a certain third party that provides a &#8220;Web service&#8221; to allow data integration into member websites. Unfortunately for me, this service revolves around plopping an <code>IFRAME</code> into your page where you&#8217;d like the data to appear. Not great.</p>
<p>In an ideal world, we&#8217;d be able to pull said content via (the real) <abbr title="Asynchronous JavaScript and XML">AJAX</abbr> but due to security (particularly cross domain) issues, that&#8217;s not a possibility. All is not lost, however. Enter <a href="http://php.net/manual/en/book.curl.php">cURL</a>.</p>
<h2>A brief introduction to cURL</h2>
<p><a href="http://php.net/manual/en/book.curl.php">cURL</a> is defined as:</p>
<blockquote cite="http://www.php.net/manual/en/intro.curl.php">
<p>PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP&#8217;s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.</p>
<p>These functions have been added in PHP 4.0.2.</p>
</blockquote>
<p>In summary, cURL allows you to have PHP fetch a page for you to do with what you will.</p>
<h3>Setting up cURL</h3>
<p>There&#8217;s a bit of a learning curve when using cURL, so you&#8217;ll want to review the manual. If you&#8217;re looking to set something up quick and dirty, the function I&#8217;ve come to use is (<a href="http://www.php.net/manual/en/ref.curl.php#93163">via</a>):</p>
<pre class="sh_php"><code>function get_url( $url,  $javascript_loop = 0, $timeout = 5 )
{
    $url = str_replace( "&amp;", "&#038;", urldecode(trim($url)) );

    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
    curl_close ( $ch );

    if ($response['http_code'] == 301 || $response['http_code'] == 302)
    {
        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

        if ( $headers = get_headers($response['url']) )
        {
            foreach( $headers as $value )
            {
                if ( substr( strtolower($value), 0, 9 ) == "location:" )
                    return get_url( trim( substr( $value, 9, strlen($value) ) ) );
            }
        }
    }

    if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &#038;&#038;
            $javascript_loop < 5
    )
    {
        return get_url( $value[1], $javascript_loop+1 );
    }
    else
    {
        return array( $content, $response );
    }
}</code></pre>
<p>This function allows me to pass a URL and have it be returned as the first index in an array. The second index contains another array of response headers as well.</p>
<h2>Replacing an IFRAME with cURL</h2>
<p>The particular service I'm working with uses <code>GET</code> variables to filter the data presented. I can literally use the same URL string in my cURL function and work with the data straight away. For example:</p>
<pre class="sh_php"><code>$service_url  = $service_base_url;
$service_url .= &quot;&amp;var1=X&quot;;
$service_url .= &quot;&amp;var2=Y&quot;;
$service_url .= &quot;&amp;api_key=&quot; . $service_api_key;

$request_results = get_url($service_url);

preg_match(&quot;/&lt;body.*\/body&gt;/s&quot;, $request_results[0], $pagecontent);

$pagecontent = $pagecontent[0];

$pagecontent = str_replace(&#x27;&lt;body&gt;&#x27;, &#x27;&#x27;, $pagecontent);
$pagecontent = str_replace(&#x27;&lt;/body&gt;&#x27;, &#x27;&#x27;, $pagecontent);

// I'd like to resize the images...
$pattern = &#x27;/\&lt; *[img][^\&gt;]*[src] *= *[\&quot;\&#x27;]{0,1}([^\&quot;\&#x27;\ &gt;]*)/i&#x27;;
$replacement = &#x27;&lt;img src=&quot;&#x27; . $imgpath . &#x27;/phpthumb/phpThumb.php?src=&#x27; . &#x27;$1&#x27; . &#x27;&amp;w=160&amp;h=110&amp;zc=1&#x27;;
$pagecontent = preg_replace($pattern, $replacement, $pagecontent);

echo $pagecontent;</code></pre>
<p>What's happening there is I'm first building the request URL (as the <code>GET</code> variables will change based on a few things) and then firing my <code>get_url()</code> function and passing the final URL. That's a great start, but of course the cURL request is going to return a full HTML document (including the <code>head</code>) which we don't need. A quick <code>preg_match</code> will pull out everything included within the <code>body</code> of the document, and we'll finally strip that out as well.</p>
<p>That leaves you with the remote page as would have been included in the <code>IFRAME</code> itself. You can write applicable CSS and do what you will with the markup. You can even go a step further and continue to refine the markup returned. In my case, I'd like to resize the images returned to fit the design I'm trying to implement. I've come to use <a href="http://phpthumb.sourceforge.net/">phpThumb</a> for all of my resizing needs and a quick <code>preg_replace</code> lets you reformat the <code>img src</code> to better match your design.</p>
<h3>Keep in mind the terms of service</h3>
<p>I'm currently waiting to hear back from the third party in an effort of following their terms of service. With the official documentation revolving around the inclusion of an <code>IFRAME</code> I'd like to make sure that this alternative method is acceptable before I put the remaining hours into customizing the output.</p>
<br /><p><a href='http://rss.buysellads.com/click.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=632&c=1948165181' target='_blank' rel='nofollow'>
				<img src='http://rss.buysellads.com/img.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=632&c=1948165181' border='0' alt='' /></a></p><p><a href='http://buysellads.com/buy/sitedetails/pubkey/2ee344414ac81fbb0f9de6ab08e9831e/zone/1269068' target='_blank'>Advertise here with BSA</a></p>]]></content:encoded>
			<wfw:commentRss>http://mondaybynoon.com/20091123/avoiding-iframes-via-php-and-curl/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Database Caching 1/48 queries in 0.053 seconds using apc
Object Caching 468/520 objects using apc

Served from: www.mondaybynoon.com @ 2012-02-10 18:57:14 -->
