【问题标题】:json_encode($response ); how handle Json API, extract json format with Laraveljson_encode($response);如何处理 Json API,用 Laravel 提取 json 格式
【发布时间】:2019-04-13 20:12:07
【问题描述】:

我被 JSON 卡住了。

我可以使用 Guzzle 通过 API GET 响应接收数据。

我正在尝试使用 Laravel 5.7 将数据提取为 key:value 格式, 能够将其保存为 mysql db,即 5.55 版(不理解 JSON 格式)

并使用能够向最终用户展示的 Blade 来处理它。

 use GuzzleHttp\Psr7\Response;

/// 得到响应 ///

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

JSON 响应格式为:

{
  "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) 试图从数组中提取公司信息“结果”。

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

刚刚得到错误:类 App\PRHData 不存在,这是相同的 文件有代码。

2) 试图从数组中提取公司信息“结果”。

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

我收到错误:

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

我不知道那个错误信息是什么。


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

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

/// 发送请求 /// 这部分有效,我通过 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

/// 发送请求结束 ///

/// 得到响应 ///

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

1) 我正在寻找在“结果”数组下获取信息的方法 键:值格式如:

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

等等,以便能够保存它们。

到目前为止响应的输出:

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

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

"name":"Company Ltd",

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

"companyForm":"Ltd",

感谢 MikroMike

【问题讨论】:

    标签: php json laravel-5 guzzle


    【解决方案1】:

    您可以使用json_decode 的结果访问它,它将 JSON 解析为 PHP 对象/数组:

    //...
    $data = json_decode( (string) $response->getBody() );
    
    $VatId = $data->results[0]->businessId;
    

    请注意,这只会访问索引 0 处的第一家公司。您可以使用 foreach 循环遍历每个结果:

    //...
    $data = json_decode( (string) $response->getBody() );
    
    foreach ($data->results as $company) {
        $VatId = $company->businessId;
        // Do something with it in the iteration
    }
    

    【讨论】:

    • 就是这样,效果很好,谢谢。这又给我提出了一个问题:-当有多个公司信息时,我需要使用像这样的 Fofeach 循环 foreach($data->results[0]->businessId as $i => $v) { echo $v ['businessId'].'
      ';给我错误:“为 foreach() 提供的参数无效”
    • @MikroMike 你需要foreach results 数组。我用一个例子更新了答案。
    • 您作为问题的作者必须将我的答案检查为“已接受”@mikromike
    猜你喜欢
    • 2020-03-30
    • 2017-03-21
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多