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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?