заголовок (‘Content-Disposition: attachment’) не срабатывает при использовании с curl

Я экспортирую некоторые счета в JSON в моем XmlController.php и преобразовать их в XML в моем xml_export.php

В моем XmlController.php:

private function exportBills() {
$bill_ids = $this->_getParam('bill_ids');
$bill_model = new Model_Bills();
$bills = $bill_model->findByIds($bill_ids);
$data = array('bills' => array());

$extract = array();
foreach($bills as $bill) {
$data['bills'][] = $bill->getJsonView();
$extract[$bill->bill_reference_number] = $bill;
}

try {
$response = $this->post('xml_export.php', $data);
$this->getResponse()->setHttpResponseCode(200);
return;
} catch (Exception $e) {
$this->view->error = 'Unknown Error';
$this->view->description = $e->getMessage();
$this->getResponse()->setHttpResponseCode(500);
return;
}
}

private function post($url, $data) {
$data = json_encode($data);
$headers = array_merge(
array(
'Accept: application/xml',
'Content-Length: ' . strlen($data),
'Origin: ' . get_base_url()));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);

$xml = curl_exec($curl);
curl_close($curl);
if(false === $xml) {
throw new Exception('cURL failed! URL was: ' . $url);
}
}

И по моему xml_export.php:

require_once('../library/functions.php');
$json = file_get_contents('php://input');

$date = date('Y-m-d H:i:s');

try{
$input = json_decode($json, true);
$response = array();
$xml_string = null;
$xml_concat = null;
if(implode('', array_keys($input)) === 'invoices') {
foreach ($input['invoices'] as $invoice) {
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
array_to_xml($invoice, $xml_data);
$xml = $xml_data->asXML();
$xml = preg_replace('~<(\d)~', '<number$1', $xml);
$xml = preg_replace('~<\/(\d)~', '</number$1', $xml);

$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
$domxml->loadXML($xml);
$xml_string = $domxml->saveXML();

$xml_concat .= $xml_string;
}
}elseif(implode('', array_keys($input)) === 'bills') {
foreach ($input['bills'] as $bill) {
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
array_to_xml($bill, $xml_data);
$xml = $xml_data->asXML();
$xml = preg_replace('~<(\d)~', '<number$1', $xml);
$xml = preg_replace('~<\/(\d)~', '</number$1', $xml);

$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
$domxml->loadXML($xml);
$xml_string = $domxml->saveXML();

$xml_concat .= $xml_string;
}
}

file_put_contents('/tmp/report.xml', print_r(htmlspecialchars($xml_concat), true));

header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="report.xml"');
echo $xml_concat;
readfile('/tmp/report.xml');
exit;

}catch(OAuthException $e){
die('Unable to export.  Please contact support for assistance');
}

мой $xml_concat определенно имеет выход, и мой /tmp/report.xml также существует и имеет выход, но загрузка не была инициирована.

Ниже заголовок моего ответа:

HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-  check=0
Content-Type: application/json
Date: Sat, 26 Mar 2016 14:47:22 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Server: nginx
Vary: Accept
X-Powered-By: PHP/5.6.16-1+deb.sury.org~trusty+1
Content-Length: 0
Connection: keep-alive

и мой заголовок запроса:

POST /v1/xml HTTP/1.1
Connection: keep-alive
Content-Length: 81
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.108 Safari/537.36
Content-Type: application/json
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

Я не совсем уверен, почему это в application/json когда я указал в application/xml в моем контроллере и заголовках не подобрать контент-диспозицию.

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …