<?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>Michael&#039;s Blog &#187; programming</title>
	<atom:link href="http://www.michaeltozzo.com/blog/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michaeltozzo.com/blog</link>
	<description>More marklar for your marklar.</description>
	<lastBuildDate>Tue, 17 Jan 2012 17:13:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Working with CSV files in PHP</title>
		<link>http://www.michaeltozzo.com/blog/2012/01/04/working-with-csv-files-in-php/</link>
		<comments>http://www.michaeltozzo.com/blog/2012/01/04/working-with-csv-files-in-php/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 21:38:17 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[General Rambling]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.michaeltozzo.com/blog/?p=452</guid>
		<description><![CDATA[This is the best and simplest way to work with a CSV file that has the headers on the first row in PHP. if (($handle = fopen('csv.csv', 'r')) !== false) { $length = 0; $delimiter = ','; $enclosure = '"'; &#8230; <a href="http://www.michaeltozzo.com/blog/2012/01/04/working-with-csv-files-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is the best and simplest way to work with a CSV file that has the headers on the first row in PHP.</p>
<p><code><br />
if (($handle = fopen('csv.csv', 'r')) !== false)<br />
{<br />
	$length = 0;<br />
	$delimiter = ',';<br />
	$enclosure = '"';<br />
	$escape = '\\';</p>
<p>	if(($header_line = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== false)<br />
	{<br />
		while(($line = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== false)<br />
		{<br />
			$data = array_combine($header_line, $line);<br />
			//do stuff here<br />
		}<br />
	}</p>
<p>	fclose($handle);<br />
}<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaeltozzo.com/blog/2012/01/04/working-with-csv-files-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>curl function</title>
		<link>http://www.michaeltozzo.com/blog/2011/01/12/curl-function/</link>
		<comments>http://www.michaeltozzo.com/blog/2011/01/12/curl-function/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 17:53:53 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[General Rambling]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.michaeltozzo.com/blog/?p=253</guid>
		<description><![CDATA[My plan for 2011 is to mix in more programming related posts. I figure the best place to start is a powerful PHP function that I wrote that uses curl and has helped me many many times. function grab($url, $postparams &#8230; <a href="http://www.michaeltozzo.com/blog/2011/01/12/curl-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My plan for 2011 is to mix in more programming related posts. I figure the best place to start is a powerful PHP function that I wrote that uses curl and has helped me many many times.</p>
<pre><code>function grab($url, $postparams = '', $cookiefilepath = '', $referer = '')
{
  sleep(2);

  $postheaders = array();

  $url_parts = parse_url($url);

  $postheaders[] = 'Host: ' . $url_parts['host'];
  $postheaders[] = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; ' .
    'rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9';
  $postheaders[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/' .
    'html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
  $postheaders[] = 'Accept-Language: en-us,en;q=0.5';
  $postheaders[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
  $postheaders[] = 'Keep-Alive: 300';
  $postheaders[] = 'Connection: keep-alive';

  if($referer != '')
  {
    $postheaders[] = "Referer: $referer";
  }

  if($postparams != '')
  {
    $postheaders[] = 'Content-Type: application/x-www-form-urlencoded';
    $postheaders[] = "Content-length: " . strlen($postparams);
  }

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $postheaders);  // $headers

  if($postparams != '')
  {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postparams);
  }

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_URL, $url);

  if ($cookiefilepath != '')
  {
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefilepath);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefilepath);
  }
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $got_page = curl_exec($ch);
  $headers = curl_getinfo($ch);
  $error = curl_error($ch);

  curl_close($ch);

  $retval = array('result' => $got_page, 'headers' => $headers, 'error' => $error);

  return $retval;
}</code></pre>
<p>This function is great for writing crawlers as you can sent over POST variables, it can pass over and store cookies in a cookiejar and passes over a &#8220;User-Agent&#8221; header so site you&#8217;re connecting to can&#8217;t tell that you&#8217;re aren&#8217;t really a browser and a referrer so you can really mimic regular page navigation <img src='http://www.michaeltozzo.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaeltozzo.com/blog/2011/01/12/curl-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB / PHP problem</title>
		<link>http://www.michaeltozzo.com/blog/2010/02/19/mongodb-php-problem/</link>
		<comments>http://www.michaeltozzo.com/blog/2010/02/19/mongodb-php-problem/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 14:35:10 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[General Rambling]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.michaeltozzo.com/blog/?p=168</guid>
		<description><![CDATA[While at work I was looking into something that we could replace MySQL with since the two trends these days are to not do any JOINS between tables and to work with large datasets. I came across a database called &#8230; <a href="http://www.michaeltozzo.com/blog/2010/02/19/mongodb-php-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While at work I was looking into something that we could replace MySQL with since the two trends these days are to not do any JOINS between tables and to work with large datasets. I came across a database called <a href="http://www.mongodb.org">MongoDB</a> which was apparently very good at both of these things and used guides on the internet to get it going on a local Linux box. </p>
<p>I got to a point where it was almost working 100% but for some reason my simple test script couldn&#8217;t connect when run by Apache but could when run via command line. I would get the error:</p>
<p><code>PHP Fatal error:  Uncaught exception 'MongoConnectionException' with message 'Permission denied'</code></p>
<p>I scoured the internet and found only a single reference to my problem but couldn&#8217;t find a solution. After direct emails with <a href="http://snailinaturtleneck.com/toons/?page_id=2">Kristina Chodorow</a> the primary maintainer for the <a href="http://www.mongodb.org">MongoDB</a> <a href="http://search.cpan.org/dist/MongoDB">Perl</a> and <a href="http://pecl.php.net/package/mongo">PHP</a> drivers I found that the solution to my problem was to run the command:</p>
<p><code>$ /usr/sbin/setsebool -P httpd_can_network_connect 1</code></p>
<p>Hopefully this helps someone else. Thanks again Kristina <img src='http://www.michaeltozzo.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaeltozzo.com/blog/2010/02/19/mongodb-php-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

