【发布时间】:2023-12-16 07:28:02
【问题描述】:
我从外部公司得到一个数组来在我的 laravel 应用程序中处理
传入的 json 中包含类似 'bns:OrderId' 的名称?
当我尝试访问 {{ $order->bns:OrderId }}
时刀片出错我该如何处理??
控制器:
public function getBolOrders()
{
// or live API: https://plazaapi.bol.com
$url = 'https://test-plazaapi.bol.com';
$uri = '/services/rest/orders/v1/open';
// your public key
$public_key = '<public key>';
//your private key
$private_key = '<private key>';
$port = 443;
$contenttype = 'application/xml';
$date = gmdate('D, d M Y H:i:s T');
$httpmethod = 'GET';
$signature_string = $httpmethod . "\n\n";
$signature_string .= $contenttype . "\n";
$signature_string .= $date."\n";
$signature_string .= "x-bol-date:" . $date . "\n";
$signature_string .= $uri;
$signature = $public_key.':'.base64_encode(hash_hmac('SHA256', $signature_string, $private_key, true));
// Setup CURL (One can also opt to use sockets or http libraries, but CURL is a versatile, widespread solution)
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: ".$contenttype, "X-BOL-Date:".$date, "X-BOL-Authorization: ".$signature));
curl_setopt($ch, CURLOPT_URL, $url.$uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
$orders = fopen("orders.xml", "w");
fwrite($orders, $result);
fclose($orders);
if(curl_errno($ch)) {
print_r(curl_errno($ch), true);
}
// Clean up after ourselves
curl_close($ch);
// Convert XML TO JSON
$xmlNode = simplexml_load_file('orders.xml');
$arrayData = xmlToArray($xmlNode);
$OpenOrder = $arrayData['OpenOrders']['bns:OpenOrder'];
// dd($OpenOrder);
// Goto view
return view('bol.open-orders', compact('OpenOrder'));
}
从数组中添加:
array:2 [▼
0 => array:6 [▼
"bns:OrderId" => "123"
"bns:DateTimeCustomer" => "2016-11-07T15:20:08.904"
"bns:DateTimeDropShipper" => "2016-11-07T15:20:08.904"
"bns:Paid" => "true"
"bns:Buyer" => array:2 [▶]
"bns:OpenOrderItems" => array:1 [▶]
]
1 => array:6 [▼
"bns:OrderId" => "321"
"bns:DateTimeCustomer" => "2016-11-07T15:20:08.904"
"bns:DateTimeDropShipper" => "2016-11-07T15:20:08.904"
"bns:Paid" => "false"
"bns:Buyer" => array:2 [▶]
"bns:OpenOrderItems" => array:1 [▶]
]
]
刀片模板示例:
@foreach ($OpenOrder as $order)
{{ $order->bns:OrderId }}
@endforeach
【问题讨论】:
-
提供您的控制器和完整的查询。显示
dd($OpenOrder);数据。 -
更改了问题文本
-
请不要分享您的凭据,例如:公共区域的公钥和私钥.. :)
标签: php laravel laravel-5 blade laravel-blade