我正在尝试下载并解压缩.gz文件。但是,我的逻辑似乎是正确的,并且不会产生任何错误。但是,该文件未显示在服务器上(.gz也不显示)。
我尝试将其拆分为多个部分并运行代码,但是第一步甚至都没有得到.gz。
一个主意我的代码有什么问题吗?
<?php
$key = $_GET['key'];
$your_key = '0010111018474';
if ($key == $your_key)
{
echo 'Your key '. $your_key . ' has been matched with the system key '. $key . '<br>';
$url = "https://odds.smarkets.com/oddsfeed.xml.gz?event_type=horse_racing_race";
$zipFile = "smarketOdds.gz"; // Local Zip File Path
$zipResource = fopen($zipFile, "w");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
if(!$page) {
echo "Error :- ".curl_error($ch);
}
curl_close($ch);
echo "Odds have been downloaded from Smarkets.<br>";
//This input should be from somewhere else, hard-coded in this example
// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '.xml', $zipFile);
// Open our files (in binary mode)
$file = gzopen($zipFile, 'rb');
$out_file = fopen($out_file_name, 'wb');
// Keep repeating until the end of the input file
while (!gzeof($file)) {
// Read buffer-size bytes
// Both fwrite and gzread and binary-safe
fwrite($out_file, gzread($file, $buffer_size));
}
// Files are done, close files
fclose($out_file);
gzclose($file);
echo "Smarkets XML file has been extracted to server.";
}
else
{
echo "invalid key.";
}
?>
答案 0 :(得分:0)
我已经尝试过并且可以正常工作:
<?php
$url = "https://odds.smarkets.com/oddsfeed.xml.gz?event_type=horse_racing_race";
$zipFile = "smarketOdds.gz"; // Local Zip File Path
$zipResource = fopen($zipFile, "w");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
fclose($zipResource);
if($page) {
echo "file size: " . filesize($zipFile);
}
输出:
file size: 55458
可能的问题:
您未能创建自己的smarketOdds.gz
,检查您是否没有正确的权限,或者只是使用“ /tmp/smarketOdds.gz”
您超时(10秒后)?