xml到csv - 标题

时间:2017-08-26 06:41:30

标签: xml csv

有人可以帮我吗?

如何从xml向csv添加标头?我不是一个编程人员,因此,我不知道,怎么做。

非常感谢。 对不起我的英语不好。 彼得 我的PHP代码: `

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://velkoobchod.carero.sk/feed/zbozi.aspx",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $file = fopen("./csv/sklad.xml", 'w+');
fwrite($file, $response);
fclose($file);
}
$filexml='./csv/sklad.xml';
if (file_exists($filexml)) {
  $xml = simplexml_load_file($filexml);
  $f = fopen('./csv/sklad.csv', 'w');
  foreach($xml->SHOPITEM as $shopitem) {

    $values = array(
   "PRODUCT" => (string)$shopitem->PRODUCT, 
   "KOD" => (string)$shopitem->KOD, 
   "SKLADOVOST" => (string)$shopitem->SKLADOVOST
);

    fputcsv($f, $values,',','"');
  }
  fclose($f);
}


?>

1 个答案:

答案 0 :(得分:0)

您可以在for循环上方添加以下内容:

$values = array(
  "PRODUCT" => "PRODUCT", 
  "KOD" => "KOD", 
  "SKLADOVOST" => "SKLADOVOST"
);
fputcsv($f, $values,',','"');
//your existing code:
foreach($xml->SHOPITEM as $shopitem) {
...