<?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>Matt J. Wilson &#187; PHP</title>
	<atom:link href="http://www.mattjwilson.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mattjwilson.com/blog</link>
	<description>General ramblings, programming frustrations, and complete randomness.  Have questions? E-mail me.</description>
	<lastBuildDate>Tue, 19 Jan 2010 19:37:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mimic a Login with CURL</title>
		<link>http://www.mattjwilson.com/blog/2009/06/11/mimic-a-login-with-curl/</link>
		<comments>http://www.mattjwilson.com/blog/2009/06/11/mimic-a-login-with-curl/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 14:53:52 +0000</pubDate>
		<dc:creator>Matt J. Wilson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[CURL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mattjwilson.com/blog/2009/06/11/mimic-a-login-with-curl/</guid>
		<description><![CDATA[Maybe this concept is much easier for others, but I had a hard time figuring out how to properly use CURL and PHP to mimic a login.  I&#8217;ll go ahead and post the code and then describe how you can figure out exactly what you should be doing to mimic a website&#8217;s login process:

&#60;?
$ch = [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe this concept is much easier for others, but I had a hard time figuring out how to properly use CURL and PHP to mimic a login.  I&#8217;ll go ahead and post the code and then describe how you can figure out exactly what you should be doing to mimic a website&#8217;s login process:</p>
<pre class="brush: php;">
&lt;?
$ch = curl_init();

// Let's set the URL of where we want the form to POST to
curl_setopt($ch, CURLOPT_URL, 'http://www.somewebsite.com/login');

// Set the referring page
curl_setopt ($ch, CURLOPT_REFERER, &quot;http://www.somewebsite.com&quot;);

// Make sure we enable the POST
curl_setopt ($ch, CURLOPT_POST, 1);

// Set the parameters for the POST fields
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'action=login&amp;user=Someuser&amp;password=Somepassword&amp;submit=Login');

// This is the key line here.  This will mimic a cookie on our machine, but instead
// will save it to the local directory of this script in a cookie.txt file.
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

// We don't want it to print out the results for this, so set
// this option to 1
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// Execute our login request
curl_exec ($ch);

// Now we should be able to go wherever we want since we've mimiced a login
// by using curl and a 'cookie.txt' cookie jar file
$target_url = &quot;http://www.website.com/the_page_we_want_to_visit_after_login.php&quot;;
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_exec($ch);
?&gt;
</pre>
<p>To figure out what pages to visit and exactly what items were being posted to the above page, I used a nice plugin for Mozilla called <a href="http://livehttpheaders.mozdev.org/">LiveHTTPHeaders</a>.  The easiest way to detect all of the URL&#8217;s, POST&#8217;ed variables, and any other checks that a login process does is to use this add-on to capture all of that information and then mimic it with the CURL functions above.  You should be able to login and then roam around freely with your usual credentials, thanks to our cookie.txt file, which will look something like:</p>
<pre class="brush: plain;">
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

.somewebsite.com	TRUE	/	FALSE	0	PHPSESSID	9c78c16ef63ad3cb2cd9f1d00466c319
.somewebsite.com	TRUE	/	FALSE	1276117626	isAuthorized	Y
</pre>
<p>I hope someone else finds this useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mattjwilson.com/blog/2009/06/11/mimic-a-login-with-curl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading a Google Docs spreadsheet using PHP</title>
		<link>http://www.mattjwilson.com/blog/2009/06/02/reading-a-google-docs-spreadsheet-using-php/</link>
		<comments>http://www.mattjwilson.com/blog/2009/06/02/reading-a-google-docs-spreadsheet-using-php/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 20:49:08 +0000</pubDate>
		<dc:creator>Matt J. Wilson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Google Docs]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mattjwilson.com/blog/?p=12</guid>
		<description><![CDATA[So I recently came across the need to read in a Google Docs spreadhseet via PHP, which is actually incredibly easy to do.  I opted to use CSV output and had Google Docs give me a link to use.  It&#8217;s as simple as clicking &#8220;Share&#8221;, &#8220;Start Publishing&#8221;, then changing the format from &#8220;Web-Page&#8221; to &#8220;CSV [...]]]></description>
			<content:encoded><![CDATA[<p>So I recently came across the need to read in a Google Docs spreadhseet via PHP, which is actually incredibly easy to do.  I opted to use CSV output and had Google Docs give me a link to use.  It&#8217;s as simple as clicking &#8220;Share&#8221;, &#8220;Start Publishing&#8221;, then changing the format from &#8220;Web-Page&#8221; to &#8220;CSV (comma-separated values)&#8221;.  The link for this example is:</p>
<p>http://spreadsheets.google.com/pub?key=rwY-bmP3cyAzaS5xzxv3XFg&amp;output=csv</p>
<p>Now for the code; my example reads in the file and inserts into my custom &#8216;markers&#8217; table, but you should be able to get the picture from this:</p>
<pre class="brush: php;">
&lt;?
require(&quot;includes/db_conn.php&quot;);

// Opens a connection to a MySQL server
$connection = mysql_connect(&quot;localhost&quot;, $username, $password);
if (!$connection) {
die(&quot;Not connected : &quot; . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die(&quot;Can\'t use db : &quot; . mysql_error());
}

$file = fopen(&quot;http://spreadsheets.google.com/pub?key=rwY-bmP3cyAzaS5xzxv3XFg&amp;amp;output=csv&quot;,&quot;r&quot;);

//we don't want to get the first line
$firstLine = true;

$addresses = array();
$i = 0;

while (($arr = fgetcsv($file, 1000, &quot;,&quot;)) !== FALSE)
{
if ($firstLine)
{
$firstLine = false;
}
else
{
$arr = str_replace(&quot;'&quot;, &quot;\'&quot;, $arr);
$addresses[$i++] = &quot;'&quot; . $arr[1] . &quot;'&quot;;

$sql = &quot;select * from markers where address ='&quot; . $arr[1] . &quot;';&quot;;
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0)
{
$sql = &quot;insert into markers (name, address, description, type) values (&quot;.
&quot;'&quot; . $arr[0] . &quot;', '&quot; . $arr[1] . &quot;', '&quot; . $arr[2] . &quot;', '&quot; . $arr[3] . &quot;');&quot;;
mysql_query($sql);
}
else
{
$sql = &quot;UPDATE markers SET name='&quot; . $arr[0] . &quot;', description = '&quot; . $arr[2] . &quot;', type = '&quot; . $arr[3] . &quot;' WHERE address='&quot; . $arr[1] . &quot;';&quot;;
mysql_query($sql);
}
if(mysql_error())
{
echo mysql_error() .&quot;&lt;br&gt;\n&quot;;
}
}
}

// delete any addresses that existed previously, but aren't in the file now
$sql = &quot;DELETE FROM markers WHERE address NOT IN(&quot; . implode(&quot;,&quot;, $addresses) . &quot;);&quot;;
mysql_query($sql);

fclose($file)
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.mattjwilson.com/blog/2009/06/02/reading-a-google-docs-spreadsheet-using-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

