我们一直在使用3d购物车休息响应,我们已经成功到达这里是我的代码。文件名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->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);
我们在使用此网址的浏览器上有一个访问文件http://localhost/social/example.php在没有JSON编码的情况下获得JSON响应。但我希望数组这个响应,所以我们可以写CSV文件。我有很多尝试谷歌但没有更多的运气适当的解决方案。谢谢提前。
[{"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,"ItemDescription":"Tote Bag
Color: Khaki","ItemUnitPrice":1.0,"ItemWeight":3.0,"ItemOptionPrice":0.0,"ItemAdditionalField1":"","ItemAdditionalField2":"","ItemAdditionalField3":"","ItemPageAdded":"Tote-Bag_p_3.html","ItemDateAdded":"2009-06-22T12:05:07","ItemUnitCost":0.0,"ItemUnitStock":5.0,"ItemOptions":",1","ItemCatalogIDOptions":"","ItemSerial":"","ItemImage1":"http://71745179439.3dcart.net/assets/images/default/handbag.jpg","ItemImage2":"http://71745179439.3dcart.net/assets/images/default/handbag.jpg","ItemImage3":"http://71745179439.3dcart.net/assets/images/default/handbag.jpg","ItemImage4":"http://71745179439.3dcart.net/assets/images/default/handbag.jpg","ItemWarehouseLocation":"","ItemWarehouseBin":"","ItemWarehouseAisle":"","ItemWarehouseCustom":""}],"PromotionList":[],"OrderDiscount":0.0,"SalesTax":0.0,"SalesTax2":0.0,"SalesTax3":0.0,"OrderAmount":1.0,"AffiliateCommission":0.0,"TransactionList":[],"CardType":"","CardNumber":"","CardName":"John Doe","CardExpirationMonth":"","CardExpirationYear":"","CardIssueNumber":"","CardStartMonth":"","CardStartYear":"","CardAddress":"","CardVerification":"","RewardPoints":"1","QuestionList":[],"Referer":"http://www.google.com","IP":"","CustomerComments":"Sample Order from 3dcart","InternalComments":"","ExternalComments":""}]
答案 0 :(得分:0)
您可以使用json_decode
php函数:http://php.net/manual/fr/function.json-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
答案 1 :(得分:0)
我不知道您要输出的csv文件的结构是什么。如果您想要更高质量的答案,您应该在问题中提供。但请记住,您使用的是两种不同的数据结构:
因此,在json中使用嵌套数组会使这项工作更加困难。
检查您的json字符串是否有效:json validator 正如您将看到的,您提供的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
}]
}]';
使用json_decode()以这种方式将json转换为数组:第一个参数是你的json字符串,第二个是布尔值,当设置为true时将返回一个关联数组。 (manual: json_decode)
$array = json_decode($string, true);
以这种方式创建您的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字符串!
答案 2 :(得分:0)
谢谢,我必须找到一个解决方案写curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);那么curl_exec的返回值将是成功操作的实际结果,并设置false只显示浏览器