如何在php中将标题内容类型json数据写入csv文件?
我们一直在使用3d cart rest响应,我们在这里成功地得到了我的code.file name example.php,它位于我的wamp服务器中如何在php中将标题内容类型json数据写入csv文件?,php,json,rest,csv,curl,Php,Json,Rest,Csv,Curl,我们一直在使用3d cart rest响应,我们在这里成功地得到了我的code.file name example.php,它位于我的wamp服务器中 <?php $host = 'https://apirest.3dcart.com'; $version = 1; $service = 'Orders'; $secureUrl = 'https://xxxxyyyyy.3dcart.net'; // Secure URL is set in Settings->General
<?php
$host = 'https://apirest.3dcart.com';
$version = 1;
$service = 'Orders';
$secureUrl = 'https://xxxxyyyyy.3dcart.net'; // Secure URL is set in Settings->General->StoreSettings
$privateKey = 'xxxxxxxxxxxxxxxx'; // Private key is obtained when registering your app at http://devportal.3dcart.com
$token = 'xxxxxxxxxxxxx'; // The token is generated when a customer authorizes your app
// initialize cURL session
$ch = curl_init($host . '/3dCartWebAPI/v' . $version . '/' . $service);
// set headers
$httpHeader = array(
'Content-Type: application/json;charset=UTF-8',
'Accept: application/json',
'SecureUrl: ' . $secureUrl,
'PrivateKey: ' . $privateKey,
'Token: ' . $token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
// [ ... addtional cURL options as needed ... ]
$response = curl_exec($ch);
if ($response === false) {
$response = curl_error($ch);
}
curl_close($ch);
您可以使用
json\u decode
php函数:
在您的代码上:
<?php
$host = 'https://apirest.3dcart.com';
$version = 1;
$service = 'Orders';
$secureUrl = 'https://xxxxyyyyy.3dcart.net'; // Secure URL is set in Settings->General->StoreSettings
$privateKey = 'xxxxxxxxxxxxxxxx'; // Private key is obtained when registering your app at http://devportal.3dcart.com
$token = 'xxxxxxxxxxxxx'; // The token is generated when a customer authorizes your app
// initialize cURL session
$ch = curl_init($host . '/3dCartWebAPI/v' . $version . '/' . $service);
// set headers
$httpHeader = array(
'Content-Type: application/json;charset=UTF-8',
'Accept: application/json',
'SecureUrl: ' . $secureUrl,
'PrivateKey: ' . $privateKey,
'Token: ' . $token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
// [ ... addtional cURL options as needed ... ]
$response = curl_exec($ch);
if ($response === false) {
$response = curl_error($ch);
}
curl_close($ch);
$arrayResponse = json_decode($response, true);
//Do something with your array
导言
我不知道您想要输出的csv文件的结构是什么。如果你想要一个更好的答案,你应该在你的问题中提供它。请记住,您使用的是两种不同的数据结构:
- JSON是一棵树
- CSV是一张桌子
因此,在json中使用嵌套数组将使这项工作更加困难
第一步
检查您的json字符串是否有效:
正如您将看到的,您提供的json字符串响应不是
对于我回答的其余部分,我将考虑你的回答的一部分,我的回答是:
$string = '
[{
"InvoiceNumberPrefix": "AB-",
"InvoiceNumber": 1000,
"OrderID": 1,
"CustomerID": 1,
"OrderDate": "2014-01-10T12:44:37",
"OrderStatusID": 1,
"LastUpdate": "0001-01-01T00:00:00",
"UserID": "",
"SalesPerson": "",
"ContinueURL": "http://71745179439.3dcart.net/continue_order.asp?orderkey=tl31S22wts7B0hF1",
"BillingFirstName": "John",
"BillingLastName": "Doe",
"BillingCompany": "",
"BillingAddress": "123 Street",
"BillingAddress2": "",
"BillingCity": "Coral Springs",
"BillingState": "FL",
"BillingZipCode": "33065",
"BillingCountry": "US",
"BillingPhoneNumber": "800-828-6650",
"BillingEmail": "test@3dcart.com",
"BillingPaymentMethod": "Online Credit Card",
"BillingOnLinePayment": true,
"BillingPaymentMethodID": "1",
"ShipmentList": [{
"ShipmentID": 0,
"ShipmentLastUpdate": "0001-01-01T00:00:00",
"ShipmentBoxes": 1,
"ShipmentInternalComment": "Sample Order from 3dcart",
"ShipmentOrderStatus": 1,
"ShipmentAddress": "123 Street",
"ShipmentAddress2": "",
"ShipmentAlias": "",
"ShipmentCity": "Coral Springs",
"ShipmentCompany": "",
"ShipmentCost": 0.0,
"ShipmentCountry": "US",
"ShipmentEmail": "",
"ShipmentFirstName": "Test",
"ShipmentLastName": "Test",
"ShipmentMethodID": 0,
"ShipmentMethodName": "Free Shipping",
"ShipmentShippedDate": "",
"ShipmentPhone": "800-828-6650",
"ShipmentState": "FL",
"ShipmentZipCode": "33065",
"ShipmentTax": 0.0,
"ShipmentWeight": 1.0,
"ShipmentTrackingCode": "",
"ShipmentUserID": "",
"ShipmentNumber": 1,
"ShipmentAddressTypeID": 0
}],
"OrderItemList": [{
"CatalogID": 3,
"ItemIndexID": 1,
"ItemID": "1003K",
"ItemShipmentID": 0,
"ItemQuantity": 1.0,
"ItemWarehouseID": 0
}]
}]';
步骤2
使用json_decode()以这种方式将json转换为数组:第一个参数是json字符串,第二个参数是布尔值,设置为true时将返回关联数组
步骤3
通过以下方式创建您的csv文件:
$f = fopen('output.csv', 'w');
foreach ($array as $row) {
$result = [];
array_walk_recursive($row, function($item) use (&$result) {
$result[] = $item;
});
fputcsv($f, $result);
}
当然,我的答案是基于步骤1中提供的json字符串 谢谢,我所要做的就是找到一个解决方案写curl\u setopt($ch,CURLOPT\u RETURNTRANSFER,true);然后curl_exec的返回值将是成功操作的实际结果,并设置为false display it only browser您是否尝试过json_decode
?请使用json_decode($response,true)。在执行任何操作之前,请验证您的json。一个简单的步骤,显示什么是错误的。然后您可以在响应中实际使用json_decode()。谢谢…Elbarto问题不在于json数据格式,而在于如何将此数据写入direct CSV文件。此数据显示在您要求将json响应放入数组的网页上。我们告诉过你,你必须使用json_decode($json,true)进行解码。但是为了做到这一点,您必须使用一个有效的json字符串,而您没有。然后,只有您可以继续使用fputcsv()来解决您的问题。使用此URL访问
$array = json_decode($string, true);
$f = fopen('output.csv', 'w');
foreach ($array as $row) {
$result = [];
array_walk_recursive($row, function($item) use (&$result) {
$result[] = $item;
});
fputcsv($f, $result);
}