Zoho JSON 结果转换为 CSV 文件
Zoho JSON result into CSV file
我正在尝试将 JSON 结果转换为 CSV 文件,但是,我一直在尝试获取 属性 的非对象
这是代码
$url = file_get_contents('https://crm.zoho.com/crm/private/json/Contacts/getRecords?authtoken=myAPIkey&scope=crmapi');
$data = json_decode($url);
$r = $data->response->result->Contacts->row->no[1];
//Give our CSV file a name.
$csvFileName = 'example.csv';
//Open file pointer.
fopen($csvFileName,'a');
//Loop through the associative array.
foreach($r as $row) {
$values = array_column($row['columns'], 'value');
fputcsv($fp, $values);
}
//Finally, close the file pointer.
fclose($fp);
这是 JSON 响应的示例
{
"response": {
"result": {
"Contacts": {
"row": [
{
"no": "1",
"FL": [
{
"val": "CONTACTID",
"content": "3508588000000206016"
},
{
"val": "SMOWNERID",
"content": "3508588000000176021"
},
{
"val": "Contact Owner",
"content": "Chris Yates"
},
{
"val": "First Name",
"content": "Bob"
当然,我希望它遍历每一行并保存到 CSV
提前致谢,我已经查看了一些关于 SO 的解决方案,但 none 似乎对我有用
让我们检查下面的代码。
<?php
$json = '{"response":{"result":{"Contacts":{"row":[{"no":"1","FL":
[{"val":"CONTACTID","content":"3508588000000206016"},
{"val":"SMOWNERID","content":"3508588000000176021"},{"val":"Contact
Owner","content":"Chris Yates"},{"val":"First Name","content":"Bob"}]}]}}}}';
$data = json_decode($json,true);
$rows = $data['response']['result']['Contacts']['row'];
$csvFileName = 'example.csv';
$fp = fopen($csvFileName,'a');
foreach($rows as $row) {
foreach($row['FL'] as $fls) {
fputcsv($fp, $fls);
}
}
fclose($fp);
?>
我正在尝试将 JSON 结果转换为 CSV 文件,但是,我一直在尝试获取 属性 的非对象
这是代码
$url = file_get_contents('https://crm.zoho.com/crm/private/json/Contacts/getRecords?authtoken=myAPIkey&scope=crmapi');
$data = json_decode($url);
$r = $data->response->result->Contacts->row->no[1];
//Give our CSV file a name.
$csvFileName = 'example.csv';
//Open file pointer.
fopen($csvFileName,'a');
//Loop through the associative array.
foreach($r as $row) {
$values = array_column($row['columns'], 'value');
fputcsv($fp, $values);
}
//Finally, close the file pointer.
fclose($fp);
这是 JSON 响应的示例
{
"response": {
"result": {
"Contacts": {
"row": [
{
"no": "1",
"FL": [
{
"val": "CONTACTID",
"content": "3508588000000206016"
},
{
"val": "SMOWNERID",
"content": "3508588000000176021"
},
{
"val": "Contact Owner",
"content": "Chris Yates"
},
{
"val": "First Name",
"content": "Bob"
当然,我希望它遍历每一行并保存到 CSV
提前致谢,我已经查看了一些关于 SO 的解决方案,但 none 似乎对我有用
让我们检查下面的代码。
<?php
$json = '{"response":{"result":{"Contacts":{"row":[{"no":"1","FL":
[{"val":"CONTACTID","content":"3508588000000206016"},
{"val":"SMOWNERID","content":"3508588000000176021"},{"val":"Contact
Owner","content":"Chris Yates"},{"val":"First Name","content":"Bob"}]}]}}}}';
$data = json_decode($json,true);
$rows = $data['response']['result']['Contacts']['row'];
$csvFileName = 'example.csv';
$fp = fopen($csvFileName,'a');
foreach($rows as $row) {
foreach($row['FL'] as $fls) {
fputcsv($fp, $fls);
}
}
fclose($fp);
?>