I am trying to download with PHP/Curl a file from a public website for an open data project. How can I emulate the download request with PHP/Curl to obtain the file?
Can you please help me with this or least with how I should phrase the question?
The site uses javascripts to generate the download action. The download requests is done via a post-request (so no URL visible).
The site is : http://cri.nbb.be/bc9/web/catalog?lang=N&companyNr=0403233750 The file I try to download is the latest XBRL document related to the entity.
The header of the download request is the following:
Host: cri.nbb.be
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://cri.nbb.be/bc9/web/catalog?execution=e1s1
Cookie: be.nbb.selected.language=nl; JSESSIONID=00003DzVLI5-4k_otlBnJ3ylzKQ:-1; TS01f1bcac=011cb8a973def2718973d95f3988ed8392a49007ea289ef41640f86d275cfbbcc3df12bec9ffca6ced4717c1f1904a1785807d461dd198bf5951a9c35c905e55eeb738ad098adfe9ea3eef44ea3732108f528c6c5d; BIGipServerprd-bc9=270313664.46162.0000
Connection: keep-alive
I can obtain the source file that generates the download request (the htlm with the javascript) with the following code:
$filename = "0403233750.html";
$url = "http://cri.nbb.be/bc9/web/catalog?lang=N&companyNr=0403233750";
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$output = curl_exec ($ch);
if (preg_match('/expired/', $output)){
return "stop";
}
if (preg_match('/problem/', $output)){
return "stop";
}
if (!preg_match('/xml/', $output)){
return "stop";
}
file_put_contents($filename, $output);
curl_close ($ch);
But once I have the javascript, I don't know what I need to use to generate the download request in PHP/Curl.