I’ve never done anything like this at all before (I’m just a beginner but have been told to get this done by the weekend ) so am not sure I’m doing things right.
What I’m trying to do it save the compressed url on a server as whatever.xml but when I do that it’s compressed.
I can save it or I can uncompress it (using file_get_contents) but I can’t seem to do both.
I tried this (this is when I got the mtime error) but don’t understand half of it:
<?php
$file = "http://datafeed.api.productserve.com/datafeed/download/apikey/api_id_key/fid/columns/aw_product_id,merchant_product_id,merchant_category,aw_deep_link,merchant_image_url,search_price,description,product_name,merchant_deep_link,aw_image_url,merchant_name,merchant_id,category_name,category_id,delivery_cost,currency,store_price,display_price,data_feed_id,rrp_price,specifications,condition,promotional_text,warranty,merchant_thumb_url,aw_thumb_url,brand_name,brand_id,delivery_time,valid_from,valid_to,web_offer,pre_order,in_stock,stock_quantity,is_for_sale,product_type,commission_group,upc,ean,mpn,isbn,model_number,parent_product_id,language,last_updated,dimensions,colour,keywords,custom_1,custom_2,custom_3,custom_4,custom_5,saving,delivery_weight,delivery_restrictions,reviews,average_rating,number_stars,number_available,rating,alternate_image,large_image,basket_link/format/xml/dtd/1.4/compression/gzip/adultcontent/1/";
$fin = fopen($file, "rb");
if ($fin !== FALSE) {
$fout = fopen("php://output", "wb");
if ($fout !== FALSE) {
// write gzip header
fwrite($fout, "\x1F\x8B\x08\x08".pack("V", filemtime($file))."\0\xFF", 10);
// write the original file name
$oname = str_replace("\0", "", basename($file));
fwrite($fout, $oname."\0", 1+strlen($oname));
// add the deflate filter using default compression level
$fltr = stream_filter_append($fout, "zlib.deflate", STREAM_FILTER_WRITE, -1);
// set up the CRC32 hashing context
$hctx = hash_init("crc32b");
// turn off the time limit
if (!ini_get("safe_mode")) set_time_limit(0);
$con = TRUE;
$fsize = 0;
while (($con !== FALSE) && !feof($fin)) {
// deflate works best with buffers >32K
$con = fread($fin, 64 * 1024);
if ($con !== FALSE) {
hash_update($hctx, $con);
$clen = strlen($con);
$fsize += $clen;
fwrite($fout, $con, $clen);
}
}
// remove the deflate filter
stream_filter_remove($fltr);
// write the CRC32 value
// hash_final is a string, not an integer
$crc = hash_final($hctx, TRUE);
// need to reverse the hash_final string so it's little endian
fwrite($fout, $crc[3].$crc[2].$crc[1].$crc[0], 4);
// write the original uncompressed file size
fwrite($fout, pack("V", $fsize), 4);
fclose($fout);
}
fclose($fin);
}
?>
Thanks for helping me with this.