Reputation: 1940
I'm tinkering with GuzzleHTTP and API requests. I'm testing with Napiarfolyam.hu. They collect the exchange rates of different Valuta's in different Banks's.
Their API address: http://api.napiarfolyam.hu
They accept parameterised GET
requests
Possible parameters:
They say that their output should be this:
<arfolyam>
<valuta>
<item>
<bank>bank rövidítése</bank>//The bank's short name
<datum>mikor kapta ezt az értéket</datum>//Date
<penznem>pénznem kódja</penznem>//Currency
<vetel>árfolyam 1 egységre</vetel>//BuyPrice
<eladas>árfolyam 1 egységre</eladas>//SellPrce
</item>
</valuta>
<deviza>
<item>
<bank>bank rövidítése</bank>//The bank's short name
<datum>mikor kapta ezt az értéket</datum>//date
<penznem>pénznem kódja</penznem>//Currency
<vetel>árfolyam 1 egységre</vetel>//Buyprice
<eladas>árfolyam 1 egységre</eladas>//Sellprice
<kozep>árfolyam 1 egységre</kozep>//Middleprice only when the bank is MNB
</item>
</deviza>
</arfolyam>
My controller so far:
<?php
namespace App\Http\Controllers;
use DB;
use Carbon\Carbon;
use GuzzleHttp\Client;
class ValutaController extends Controller {
public function getValuta($bankName = '', $valuta = ''){
$client = new Client();
$response = $client->request('GET', "http://api.napiarfolyam.hu?bank={$bankName}&valuta={$valuta}");
$body = $response->getBody();
}
}
My problem: $body
is a string
and not an xml
. Why?
It would be better as an xml
or an array
becouse as I tinkered I saw that sometimes the BuyPrice
and SellPrice
are changed their place .
Upvotes: 0
Views: 477
Reputation: 1326
You can call Guzzle built-in xml()
function,
http://guzzle3.readthedocs.io/http-client/response.html#xml-responses
You can easily parse and use a XML response as SimpleXMLElement object using the xml() method of a response. This method will always return a SimpleXMLElement object if the response is valid XML or if the response body is empty. You will get an exception if you call this method and the response is not valid XML.
Here is your updated code
<?php
namespace App\Http\Controllers;
use DB;
use Carbon\Carbon;
use GuzzleHttp\Client;
class ValutaController extends Controller {
public function getValuta($bankName = '', $valuta = ''){
$client = new Client();
$response = $client->request('GET', "http://api.napiarfolyam.hu?bank={$bankName}&valuta={$valuta}");
$body = $response->xml();
}
}
Upvotes: 1
Reputation: 5599
Guzzle does not perform any conversion on the response, it returns the response to you as a string ready for you to perform any processing you need.
If you'd like to take a string of XML and turn it into an object then you can make use of simplexml_load_string
, e.g:
<?php
namespace App\Http\Controllers;
use DB;
use Carbon\Carbon;
use GuzzleHttp\Client;
class ValutaController extends Controller {
public function getValuta($bankName = '', $valuta = ''){
$client = new Client();
$response = $client->request('GET', "http://api.napiarfolyam.hu?bank={$bankName}&valuta={$valuta}");
$data = simplxml_load_string($response->getBody());
return $data->valuta->item->vetel;
}
}
Upvotes: 1