i exporting invoices in json in xmlcontroller.php
, converting them in xml in xml_export.php
in 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 $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); } }
and in 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'] $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'] $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 export. please contact support assistance'); }
my $xml_concat
has output , /tmp/report.xml
exists , has output download not triggered.
below reponse header:
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
and request header:
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, 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
i not sure why in application/json
when specified in application/xml
in controller , headers did not pick content-disposition.
Comments
Post a Comment