0

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:

  • bank (possible inputs: bb, allianz, cib, citibank, commerz, erste, kdb, kh, mkb, oberbank, otp, raiffeisen, unicredit, volksbank, mnb, sopron, mfb, fhb)
  • valuta (possible inputs: GBP, AUD, DKK, JPY, CAD, NOK, CHF, SEK, USD, CZK, PLN, EUR, HRK, RON, TRY)
  • datum (the date which data we want to see) in YYYYMMDD format
  • datumend (if we use it we will get the exchange rates in datum-datumend intervall) in YYYYMMDD format
  • valutanem (possible inputs valuta, deviza. We can narrow the results with it)

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 .

2 Answers 2

1

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();  

    }

    }
Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.