Api Response和Json laravel格式

时间:2019-05-12 16:56:32

标签: json foreach laravel-5.7

我正在使用Laravel 5.7。和GuzzleHttp 6.0以获得API响应 来自端点

我正在将查询数据从Blade表单传递给此函数。

public static function prhmulti($multisearch, $start ,$end)

{   $city = $multisearch['city'];

  $client = new Client([
     'base_uri' =>  'https://avoindata.prh.fi/tr/',
     'query' => [
    'totalResults' => 'true',
    'maxResults' => '1000',
    'registeredOffice'=> $city,
    'companyForm'=>'OY',
    'companyRegistrationFrom'=>$start,
    'companyRegistrationTo'=>$end,
],
       'defaults'=>[
       'timeout'  => 2.0,
       'cookies' => true,
       'headers'  => [
         'content-type' => 'application/json',
         'User-Agent' =>"GuzzleHttp/Laravel-App-5.7, Copyright MikroMike"
       ]]]);

$res = $client->request('GET','v1');
$ResData = json_decode($res->getBody()->getContents());

dd($ ResData)给出API响应中的所有数据。

但是我无法将JSON返回其他函数

return $this->multisave($ResData);



public static function multisave (data $ResData)

这将解析JSON和

{
 foreach ($data->results as $company) {
    $name = $company->name;
    $Addr = $company->addresses;
    $businessId = $company->businessId;
    $companyForm = $company->companyForm;
    $registrationDate = $company->registrationDate;

  foreach ($company->addresses as $Addr) {
      $city = $Addr->city;
      $postcode = $Addr->postCode;
      $street = $Addr->street;
   }
 }

将数据保存到Mysql。

$NewCompany = new Company();

 $NewCompany = Company::updateOrCreate($array,[
 [ 'vat_id', $businessId],
 [ 'name', $name],
 [ 'form',$companyForm],
 [ 'street', $Addr],
 [ 'postcode', $postcode],
 [ 'city', $city],
 [ 'regdate', $registrationDate],
 ]);

}

如果“解析”部分和“保存”部分在同一功能代码内,则可以正常工作(仅保存一个公司),

但是我需要将它们分开,因为以后更容易维护。

我将返回$ ResData的错误

" Using $this when not in object context" 

信息位于JSON数组中。

每个零件也只能节省一家公司吗?

  foreach ($data->results as $company) {
  $name = $company->name;
  $Addr = $company->addresses;
  $businessId = $company->businessId;
  $companyForm = $company->companyForm;
  $registrationDate = $company->registrationDate;

foreach ($company->addresses as $Addr) {
   $city = $Addr->city;
   $postcode = $Addr->postCode;
   $street = $Addr->street;
 }

所以:1)什么是创建自己的用于解析JSON的函数的最佳方法          和其他用于将数据保存到数据库?      2)由于foreach循环只保存一个公司数据,什么是          修复它的最佳方法?

感谢MikroMike。

1 个答案:

答案 0 :(得分:0)

解决了我自己的有关将公司保存到数据库的问题

  • 首先获取数组内的总数
  • 使用for循环进行计数
  • 使用每个公司的foreach循环提取信息作为对象。

    $data = json_decode($res->getBody()->getContents());
    $total = $data->totalResults;
    
    
    for ($i = 0; $i < $total; $i++){
    $NewCompany = new Company();
    
     foreach ($data->results as $company)
    {
     $name = $company->name;
     $businessId = $company->businessId;
     $companyForm = $company->companyForm;
     $registrationDate = $company->registrationDate;
    
    $array = [];
      Arr::set($array, 'vat_id', $businessId);
      Arr::set($array, 'name', $name );
      Arr::set($array, 'form', $companyForm);
      Arr::set($array, 'regdate', $registrationDate);
    
     $NewCompany = Company::updateOrCreate($array,[
    [ 'vat_id', $businessId],
    [ 'name', $name],
    [ 'form',$companyForm],
    [ 'regdate', $registrationDate],
    ]);
    
    
    }// END OF MAIN FOREACH
    }// END OF For loop
    }// END OF FUCNTION
    } // END OF CLASS