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

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

  • duandiaoqian5795 2019-04-13 20:29
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置