Rudi Werner Rudi Werner - 27 days ago 5
PHP Question

Laravel get array in blade with such 'bns:OrderId' names in it?

I get a array from a external company to handle in my laravel application

The incoming json has names like 'bns:OrderId' in it ?

blade give a error when i try to access {{ $order->bns:OrderId }}

How can i handle this??

Controller :

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 = 'PNWxYsDsesTTYqAalxHZgnXThLQlMgdQ';

//your private key
$private_key = 'RPXTsnOiKWDBTxgqGbpBpmgURAWkpiImgRwwdjtzlOezfRpcTKqGwembLwZFsVmVbjoBceTMFzQTGJMCEdrnQeHtItfbeHSyvHCERCoyRqLDfHJJMRFtXUyhkziLCoBIZtYkdstMLzJEqZZPHHADZmfeiTBvLuqSvRdUoKhaCFEaATuxlkTxcKomxobsIlUCaivJkFlOwPfDcfkWMTjbvHXBaRgaPcQNfNKpEMPGqPYtnLqUSPIVBoxAXBzxqEEc';

$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'));
}


dd from array :

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 [▶]
]
]


Example blade template :

@foreach ($OpenOrder as $order)
{{ $order->bns:OrderId }}
@endforeach

Answer

This query returns the collection of arrays values not an Object. use this {{ $order["bns:OrderId"] }} instead of this {{ $order->bns:OrderId }}.

Try this:

@foreach ($OpenOrder as $order)
    {{ $order["bns:OrderId"] }}
@endforeach