0

I'm stuck with JSON.

I can receive data via API GET response, by using Guzzle.

I'm trying to extract data to key:value format with Laravel 5.7, to able save it mysql db, which is version 5.55 (does not understand JSON format)

and handle it with Blade able to show it enduser.

 use GuzzleHttp\Psr7\Response;

/// Getting Response ///

  $data = json_decode( (string) $response->getBody() );
  echo $response->getBody() ; 

JSON Response format is:

{
  "type": "fi.prh.opendata.bis",
  "version": "1",
  "totalResults": -1,
  "resultsFrom": 0,
  "previousResultsUri": null,
  "nextResultsUri": null,
  "exceptionNoticeUri": null,
  "results": [ // <- IN this part has company information
    {
      "businessId": "0856064-3",
      "name": "Company Ltd",
      "registrationDate": "1991-09-18",
      "companyForm": "Ltd",
      "detailsUri": null,
      "liquidations": [

      ],
      "names": [ // <- other array, which has history of Company names
        {
          "order": 0,
          "version": 1,
          "name": "Company Ltd",
          "registrationDate": "1991-08-14",
          "endDate": null,
          "source": 1
        }
      ]
    }
  ]
}

1) Tried to extract company information "results": from array.

echo $response->getBody()->getContents([results]); 

Just getting ERROR: Class App\PRHData does not exist which is same file as having code.

2) Tried to extract company information "results": from array.

 foreach($data[result[0]]['businessId'] as $i => $v)
 {
 echo $v['businessId'].'<br/>';
 }

I'm getting error:

 Use of undefined constant result - assumed 'result' (this will 
 throw an Error in a future version of PHP)

I do not have any idea what is that error message.


use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7;

class PRHData extends Model
{
public static function getJson()
{

/// Sending request /// This part works, I'm getting data via API.

$client = new Client([
   'base_uri' => 'https://avoindata.prh.fi/bis/v1/',
   'defaults'=>[
     'timeout'  => 2.0,
     'cookies' => true,
     'headers'  => ['content-type' => 'application/json']]
  ]);
  $response = $client->request('GET','0856064-3'); //<- number is 
                             parameter for GET request to API call

/// End of Sending request ///

/// Getting Response ///

 $data = json_decode( (string) $response->getBody() ); // WORKS! 
 echo $response->getBody() ;   // Works !!!!

1) I'm looking for way to get information under "result" array to key:value format like:

$VatId = $value['businessId'];
$Name = $value{'name'];
$RegDate = $value['registrationDate'];
$companyForm = $value['companyForm'];

and so on, to able save them db.

Output from response so far:

"results": // <- IN this part has company information

[{"businessId":"0856064-3",

"name":"Company Ltd",

"registrationDate":"1991-09-18",

"companyForm":"Ltd",

Thanks MikroMike

1 Answer 1

0

You can access it using the result of your json_decode, which parses the JSON into PHP objects/arrays:

//...
$data = json_decode( (string) $response->getBody() );

$VatId = $data->results[0]->businessId;

Note that this will only ever access the first company at index 0. You can use a foreach loop to traverse every result:

//...
$data = json_decode( (string) $response->getBody() );

foreach ($data->results as $company) {
    $VatId = $company->businessId;
    // Do something with it in the iteration
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's the way, it works great, thanks. Which raise me one more question: - When there will be more than one company information I need to use Fofeach loop like this foreach($data->results[0]->businessId as $i => $v) { echo $v['businessId'].'<br/>'; } Gives me error: "Invalid argument supplied for foreach()"
@MikroMike you need to foreach the results array. I updated the answer with an example.
You as author of the question have to check my answer as "accepted" @mikromike

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.