duangua5308
2019-04-13 20:12
浏览 215
已采纳

json_encode($ response); 如何处理Json API,用Laravel提取json格式

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条回答 默认 最新

相关推荐 更多相似问题