[Solved] How to force file to download which is output of json file


You can use file_get_contents like this:

<?php
$details1=json_decode(file_get_contents("http://2strok.com/download/download.json"), true);
$details2=json_decode(file_get_contents($details1['data']), true);

$file_url = $details2['data'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . 
basename($file_url) . "\""); 
readfile($file_url);
?>

Or you can use cURL:

<?php
$ch = curl_init();
$url = "http://2strok.com/download/download.json";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$url2 = json_decode($output)->data;

curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$url3 = json_decode($output)->data;

curl_close($ch);     

header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($url3) . "\"");
readfile($url3);
?>

9

solved How to force file to download which is output of json file