我坚持使用JSON。
我可以使用Guzzle通过API GET响应接收数据。
我正在尝试使用Laravel 5.7将数据提取为key:value格式,以便将其保存为5.55版的mysql db(不了解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