我创建了一个php代码,以通过rest api每天从woocommerce导出订单,但是我无法以正确的方式转换csv文件中的json数据。 非常感谢您的帮助。
<?php
$url = https://www.xxxxx.com/it/wp-json/wc/v3/orders?consumer_key=ck_be00e6768gh3641895fc113739ac2a266714a35b&consumer_secret=cs_69472477jki134fcea354c83a600be8e20eaf629&after=".date("Y-m-d")."T00:00:00Z&before=".date("Y-m-d")."T23:59:59Z&page=1&orderby=id&order=asc";
$options = array(
"http" => array(
"header" => "Content-type: text/json\r\n",
"method" => "GET",
"content" => json_encode($request)
),
);
$context = stream_context_create($options);
//Decode the JSON and convert it into an associative array.
$result = json_decode(file_get_contents($url, false, $context), true);
//Give our CSV file a name.
$nome ="my-orders-". date("Y-m-d").".csv";
$csvFileName = $nome;
//Open file pointer.
$fp = fopen($csvFileName, 'w');
//Loop through the associative array.
foreach($result as $fields){
//Write the row to the CSV file.
fputcsv($fp, $fields, ',');
}
//Finally, close the file pointer.
fclose($fp);
print_r($result);
?>