items as $item) { $file_key = md5($item['link']); //get the contents of the file you want to cache $req =& new HTTP_Request($item['link']); $req->sendRequest(); $response_code = $req->getResponseCode(); $file_content_type = $req->getResponseHeader('content-type'); $file_contents = $req->getResponseBody(); //if there is a response body, create the cache if($file_contents != '') { //assemble your s3 signature $s3_signature = "PUT\n\n".$content_type."\n".$rfc_822_datetime ."\nx-amz-acl:".ACL_SETTING."\n/".BUCKET_NAME."/".$file_key; $hasher =& new Crypt_HMAC(S3_SECRET_KEY, "sha1"); $signature = hex2b64($hasher->hash($s3_signature)); //make the request to create the file in the bucket $s3req =& new HTTP_Request(S3_URL.BUCKET_NAME."/".$file_key); $s3req->setMethod('PUT'); $s3req->addHeader("content-type", $content_type); $s3req->addHeader("Date", $rfc_822_datetime); $s3req->addHeader("x-amz-acl", ACL_SETTING); $s3req->addHeader("Authorization", "AWS " . S3_ACCESS_KEY . ":" . $signature); $s3req->setBody($file_contents); $s3req->sendRequest(); if ($s3req->getResponseCode() == 200) { echo "Cache updated for ".$item['link']." (".$file_key.")\n"; } else { echo "Problem updating cache for ".$item['link']." (".$file_key.") - Status was ".$s3req->getResponseCode()."\n"; } } else { //handle any snoopy errors by outputting them to the screen echo "Problem fetching ".$item['link']." (".$file_key."): " .$req->getResponseCode()."\n"; } } function hex2b64($str) { $raw = ''; for ($i=0; $i < strlen($str); $i+=2) { $raw .= chr(hexdec(substr($str, $i, 2))); } return base64_encode($raw); } ?>