php Zamzar文件转换和下载。示例是XLS到CSV
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Zamzar文件转换和下载。示例是XLS到CSV相关的知识,希望对你有一定的参考价值。
$apiKey = ''; // Zamzar API
$url = ''; // Source URL to excel file
$tmp_file = sys_get_temp_dir() . '/zamzar_' . time().'.csv'; // Where we are saving the converted file on the server
// Start a conversion job
$job = zamzar_job_create($url, 'xls', 'csv');
if (count($job['errors'])) die('There was an issue creating the job at zamzar.');
// Check the progress and wait for conversion to complete up to 30 seconds
$job_info = array();
for ($i = 0; $i <= 15; $i++) {
$job_info = zamzar_job_info($job['id']);
if ($job_info['status'] == 'successful') break;
sleep(2);
}
if ($job_info['status'] != 'successful') die('The zamzar job either took too long or was unable to convert, last status was: ' . $job_info['status']);
$download_status = zamzar_job_download($job_info['target_files'][0]['id'], $tmp_file);
function zamzar_job_create($url, $from_format, $to_format)
{
global $apiKey;
$endpoint = "https://api.zamzar.com/v1/jobs";
$postData = array(
"source_file" => $url,
"source_format" => $from_format,
"target_format" => $to_format
);
$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
$body = curl_exec($ch);
curl_close($ch);
return json_decode($body, true);
}
function zamzar_job_info($jobID)
{
global $apiKey;
$endpoint = 'https://api.zamzar.com/v1/jobs/' . $jobID;
$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
$body = curl_exec($ch);
curl_close($ch);
return json_decode($body, true);
}
function zamzar_job_download($fileID, $out_file)
{
global $apiKey;
$endpoint = 'https://api.zamzar.com/v1/files/' . $fileID . '/content';
$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$header = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
$headers = array();
foreach (explode("\r\n", trim(substr($header, 0, $headerSize))) as $row) {
if (preg_match('/(.*?): (.*)/', $row, $matches)) {
$headers[$matches[1]] = $matches[2];
}
}
$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $headers['Location']); // API endpoint
$fh = fopen($out_file, "wb");
curl_setopt($ch, CURLOPT_FILE, $fh);
$body = curl_exec($ch);
curl_close($ch);
return $body;
}
以上是关于php Zamzar文件转换和下载。示例是XLS到CSV的主要内容,如果未能解决你的问题,请参考以下文章