<?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>Mission Data Blog &#187; php</title>
	<atom:link href="http://www.missiondata.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.missiondata.com/blog</link>
	<description>Louisville-based Web Development &#38; Software Engineering</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:58:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>S3 Streaming With PHP</title>
		<link>http://www.missiondata.com/blog/systems-integration/49/s3-streaming-with-php/</link>
		<comments>http://www.missiondata.com/blog/systems-integration/49/s3-streaming-with-php/#comments</comments>
		<pubDate>Sat, 25 Nov 2006 18:45:24 +0000</pubDate>
		<dc:creator>carsonm</dc:creator>
				<category><![CDATA[Systems Integration]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/linux/49/s3-streaming-with-php/</guid>
		<description><![CDATA[Steven pointed out that someone was looking for a way to stream to S3 using PHP and said I should figure out how to get it going. Since he made a patch to let you stream data with Ruby using S3 I figured I should do one for PHP. There may be better ways of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blogs.missiondata.com/author/steveny/">Steven</a> pointed out that someone was looking for a way to <a href="http://developer.amazonwebservices.com/connect/thread.jspa?threadID=12829&#038;tstart=0">stream to S3 using PHP</a> and said I should figure out how to get it going. Since he made a patch to let you <a href="http://blogs.missiondata.com/ruby/29/streaming-data-to-s3-with-ruby/">stream data with Ruby using S3</a> I figured I should do one for PHP. There may be better ways of doing this but since I&#8217;ve already done it with C using curl I figured that would be the fastest way.<br />
<span id="more-49"></span></p>
<p>First you will want to get the <a href="http://developer.amazonwebservices.com/connect/servlet/JiveServlet/download/24-12470-46948-845/s3.php">S3 PHP library from Amazon</a>. Then you add the following to the file:</p>
<pre>
<code>/**
 * putObjectStream -- Streams data to a bucket.
 *
 * Takes ($bucket, $key, $streamFunction, $contentType, $contentLength [,$acl, $metadataArray, $md5])
 *
 *
 * - [str] $bucket: the bucket into which file will be written
 * - [str] $key: key of written file
 * - [str] $streamFunction: function to call for data to stream
 * - [str] $contentType: file content type
 * - [str] $contentLength: file content length
 * - [str] $acl: access control policy of file (OPTIONAL: defaults to 'private')
 * - [str] $metadataArray: associative array containing user-defined metadata (name=&gt;value) (OPTIONAL)
 * - [bool] $md5: the MD5 hash of the object (OPTIONAL)
*/
function putObjectStream($bucket, $key, $streamFunction, $contentType, $contentLength, $acl, $metadataArray, $md5){
        sort($metadataArray);
        $resource = "$bucket/$key";
        $resource = urlencode($resource);
        $httpDate = gmdate("D, d M Y G:i:s T");

        $curl_inst = curl_init();

        curl_setopt ($curl_inst, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt ($curl_inst, CURLOPT_LOW_SPEED_LIMIT, 1);
        curl_setopt ($curl_inst, CURLOPT_LOW_SPEED_TIME, 180);
        curl_setopt ($curl_inst, CURLOPT_NOSIGNAL, 1);
        curl_setopt ($curl_inst, CURLOPT_READFUNCTION, $streamFunction);
        curl_setopt ($curl_inst, CURLOPT_URL, $this-&gt;serviceUrl . $resource);
        curl_setopt ($curl_inst, CURLOPT_UPLOAD, true);
        curl_setopt ($curl_inst, CURLINFO_CONTENT_LENGTH_UPLOAD, $contentLength);

        $header[] = "Date: $httpDate";
        $header[] = "Content-Type: $contentType";
        $header[] = "Content-Length: $contentLength";
        $header[] = "Expect: ";
        $header[] = "Transfer-Encoding: ";
        $header[] = "x-amz-acl: $acl";

        $MD5 = "";
        if($md5){
                $MD5 = $this-&gt;hex2b64(md5_file($filePath));
                $header[] = "Content-MD5: $MD5";
        }

        $stringToSign="PUT\n$MD5\n$contentType\n$httpDate\nx-amz-acl:$acl\n";
        foreach($metadataArray as $current){
                if($current!=""){
                        $stringToSign.="x-amz-meta-$current\n";
                        $header = substr($current,0,strpos($current,':'));
                        $meta = substr($current,strpos($current,':')+1,strlen($current));
                        $header[] = "x-amz-meta-$header: $meta";
                }
        }

        $stringToSign.="/$resource";

        $signature = $this-&gt;constructSig($stringToSign);

        $header[] = "Authorization: AWS $this-&gt;accessKeyId:$signature";

        curl_setopt($curl_inst, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl_inst, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec ($curl_inst);

        $this-&gt;responseString = $result;
        $this-&gt;responseCode = curl_getinfo($curl_inst, CURLINFO_HTTP_CODE);

        curl_close($curl_inst);
}</code>
</pre>
<p>Now you have an updated s3.php you are ready to start streaming. Be aware that the content length value needs to be correct. If it is not the correct size your upload will be either truncated (too short) or it will hang (too long). If it hangs the S3 service will timeout after a small amount of time and give you an error.</p>
<p>Here are two examples of how to use it. First with a file:</p>
<pre>
<code>&lt;?php

include 's3.php';

class MyClass
{
  var $data;

  function stream_function($handle, $fd, $length)
  {
    return fread($this-&gt;data, $length);
  }
}

$my_class_inst = new MyClass();

$fsize = filesize("/tmp/largefile.tar.gz");

$my_class_inst-&gt;data = fopen("/tmp/largefile.tar.gz", "r");

$s3_inst = new s3("access key", "private key");
$s3_inst-&gt;putObjectStream("abucket", "largefile.tar.gz", array($my_class_inst, "stream_function"), "application/x-gzip", $fsize, "public-read", array(), null);

print "Response String: " . $s3_inst-&gt;responseString . "\n";
print "Response Code: " . $s3_inst-&gt;responseCode . "\n";
print "Parsed XML: " . $s3_inst-&gt;parsed_xml . "\n";

fclose($my_class_inst-&gt;data);

?&gt;</code>
</pre>
<p>Another example of streaming some random text:</p>
<pre>
<code>&lt;?php

include 's3.php';

class MyClass
{
  var $data;

  function stream_function($handle, $fd, $length)
  {
    return $this-&gt;data;
  }
}

$my_class_inst = new MyClass();

$my_class_inst-&gt;data = "A test string";
$size = strlen($my_class_inst-&gt;data);

$s3_inst = new s3("access key", "private key");
$s3_inst-&gt;putObjectStream("bucketname", "test.txt", array($my_class_inst, "stream_function"), "text/html", $size, "public-read", array(), null);

print "Response String: " . $s3_inst-&gt;responseString . "\n";
print "Response Code: " . $s3_inst-&gt;responseCode . "\n";
print "Parsed XML: " . $s3_inst-&gt;parsed_xml . "\n";

?&gt;</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/systems-integration/49/s3-streaming-with-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating S3 URLs that expire using PHP</title>
		<link>http://www.missiondata.com/blog/systems-integration/57/creating-s3-urls-that-expire-using-php/</link>
		<comments>http://www.missiondata.com/blog/systems-integration/57/creating-s3-urls-that-expire-using-php/#comments</comments>
		<pubDate>Thu, 01 Jun 2006 11:50:24 +0000</pubDate>
		<dc:creator>carsonm</dc:creator>
				<category><![CDATA[Systems Integration]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/?p=57</guid>
		<description><![CDATA[After reading this post on the S3 forum I realized that other people are thinking about doing some of the same stuff I have. paolonew was looking for a way to for a way to create URLs to S3 objects that expired. I did this a while back when I was thinking about how to [...]]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://developer.amazonwebservices.com/connect/thread.jspa?threadID=10726&#038;tstart=0">this post on the S3 forum</a> I realized that other people are thinking about doing some of the same stuff I have. paolonew was looking for a way to for a way to create URLs to S3 objects that expired. I did this a while back when I was thinking about how to host objects that I wanted to protect with some outside scheme. The confusion on the forum seemed to be about the timestamps used to expire the URL. For PHP it is fairly easy.</p>
<p>To clear up the expiration time issue I think these two steps are needed:</p>
<p>1) Keep in mind that the HTTP header dates <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html">must</a> be in GMT.<br />
2) The PHP function <a href="http://us3.php.net/manual/en/function.time.php">time()</a> returns the seconds since the epoch January 1 1970 00:00:00 GMT). Notice here this is in GMT as well.<br />
3) The HTTP Date header you see in a response from an S3 server is the time on that server. The machine you use to sign your request should be synced with that time. I think a good guess is that all the Amazon servers are synced with the atomic clock.</p>
<p>There isn&#8217;t much to securing a URL after you have that tucked away. Here is an example that will sign a URL so that it is valid for 60 seconds:</p>
<pre>
<code>&lt;?php

require_once('Crypt/HMAC.php');

echo getS3Redirect("/test.jpg") . "\\n";

function getS3Redirect($objectName)
{
  $S3_URL = "http://s3.amazonaws.com";
  $keyId = "your key";
  $secretKey = "your secret";
  $expires = time() + 60;
  $bucketName = "/your bucket";

  $stringToSign = "GET\\n\\n\\n$expires\\n$bucketName$objectName";
  $hasher =&amp; new Crypt_HMAC($secretKey, "sha1");
  $sig = urlencode(hex2b64($hasher-&gt;hash($stringToSign)));

  return "$S3_URL$bucketName$objectName?AWSAccessKeyId=$keyId&amp;Expires=$expires&amp;Signature=$sig";
}

function hex2b64($str)
{
    $raw = '';
    for ($i=0; $i &lt; strlen($str); $i+=2)
    {
        $raw .= chr(hexdec(substr($str, $i, 2)));
    }
    return base64_encode($raw);
}

?&gt;</code>
</pre>
<p>The hex2b64 function was pulled from the amazon S3 PHP example library.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/systems-integration/57/creating-s3-urls-that-expire-using-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to build the PHP rrdtool extension by hand</title>
		<link>http://www.missiondata.com/blog/system-administration/54/how-to-build-the-php-rrdtool-extention-by-hand/</link>
		<comments>http://www.missiondata.com/blog/system-administration/54/how-to-build-the-php-rrdtool-extention-by-hand/#comments</comments>
		<pubDate>Tue, 09 May 2006 12:17:28 +0000</pubDate>
		<dc:creator>carsonm</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/?p=54</guid>
		<description><![CDATA[I think by now most sysadmin types know about rrdtool and the nice graphs it makes. I recently wanted to create some graphs by hand using PHP so I turned to the php-rrdtool extension. I found that it takes a little work to get it to compile but that could be because I&#8217;m not constantly [...]]]></description>
			<content:encoded><![CDATA[<p>I think by now most sysadmin types know about <a href="http://oss.oetiker.ch/rrdtool/">rrdtool</a> and the nice graphs it makes. I recently wanted to create some graphs by hand using PHP so I turned to the php-rrdtool extension. I found that it takes a little work to get it to compile but that could be because I&#8217;m not constantly recompiling PHP and just don&#8217;t know better. You can get this module as an rpm for fedora (php-rrdtool) but I like to compile php by hand so I couldn&#8217;t use it. I&#8217;m going to assume that you know how to compile PHP normally with whatever other items you want to include and that you have the rrdtool development libraries installed or have compiled and installed rrdtool from source.</p>
<h3>Step 1. Get the PHP rrdtool source</h3>
<p>Go to the contrib directory on the rrdtool distribution site:<br />
<a href="http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/contrib/">http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/contrib/</a></p>
<p>There are a number of files in this directory that mention rrd. You want the one named: <a href="http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/contrib/php_rrdtool.tgz">php_rrdtool.tgz</a> <br/></p>
<p><span id="more-54"></span></p>
<h3>Step 2. Untar into the correct place</h3>
<p>Now that you have the source go into your php source directory and then into the ext directory. So you will be somewhere like this:</p>
<p>/usr/local/src/php-5.1.3/ext/</p>
<p>Now untar the source into this directory.</p>
<h3>Step 3. Recreate the php configuration file</h3>
<p>There is a warning that you will get if you do not have autoconf 2.13 installed on your system when you try to do this. It is easy enough to get this version if you have fedora so that is what I did. </p>
<p>One tricky part to this is that I had to remove the old configuration file first before the new one could be created.</p>
<ol>
<li>Change directory to your PHP source, if you are still in the ext directory just cd ..</li>
<li>Remove the existing configuration file</li>
<li>If you are using autoconf 2.13 run the following command: <br/> PHP_AUTOCONF=autoconf-2.13 ./buildconf &#8211;force<br/> If you are using whatever other autoconf you have installed just run: <br/> ./buildconf &#8211;force</li>
<li>You should now have a new configuration file that can be run with the &#8211;with-rrdtool option</li>
</ol>
<h3>Step 4. Test</h3>
<p>After compiling with rrdtool you should be able to use the phpinfo() function to list the installed extensions. If everything went right you should see rrdtool listed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/system-administration/54/how-to-build-the-php-rrdtool-extention-by-hand/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

