试图获取非对象的属性 - Laravel-5.5插入db

时间:2017-12-14 10:50:18

标签: php laravel laravel-5 laravel-5.5

我已经使用以下代码多次插入到数据库中,但是它随机地为我提供了尝试获取非对象属性的错误异常。 此外,有时它无法识别$ test-> data [] (由错误 [ErrorException]未定义属性:stdClass :: $ data) 我理解大多数对这个问题的回答建议使用$ test ['data']但是你可以看到我的响应体在一个数组中有几个数组,我需要访问非常具体的值。

 for($j=1;$j<50;$j++)
   {    
        sleep(30);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "specified URL");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        $response = curl_exec($ch);
        curl_close($ch);

        $test=json_decode($response,true); 

        $size=count($test['data']);

        for($i=0;$i<$size;$i++)
        { 

        $start_date=date_create($test->data[$i]->opportunity->earliest_start_date);
        $end_date=date_create($test->data[$i]->opportunity->latest_end_date);
        $app_close_date=date_create($test->data[$i]->opportunity->applications_close_date);
        $date_approved=date_create($test->data[$i]->date_approved);
        $date_realized=date_create($test->data[$i]->date_realized);
        $experience_start_date=date_create($test->data[$i]->experience_start_date);
        $experience_end_date=date_create($test->data[$i]->experience_end_date);
        $signup_date=date_create($test->data[$i]->created_at);

        DB::insert('insert ignore into Opportunity (opportunity_id,opportunity_location,office_id,programme_name,title,earliest_start_date,latest_end_date,applications_close_date) values (?, ?, ?, ?,?, ?, ?, ?)', [$test->data[$i]->opportunity->id,$test->data[$i]->opportunity->location, $test->data[$i]->opportunity->office->id, $test->data[$i]->opportunity->programmes->short_name,$test->data[$i]->opportunity->title,date_format($start_date,"Y-m-d H:i:s"),date_format($end_date,"Y-m-d H:i:s"),date_format($app_close_date,"Y-m-d H:i:s")]);
        DB::insert('insert ignore into Home_LC (id, country, name) values (?, ?, ?)', [$test->data[$i]->person->home_lc->id,$test->data[$i]->person->home_lc->country,$test->data[$i]->person->home_lc->name]);
        DB::insert('insert ignore into Applications (app_id,ep_id,opportunity_id,date_approved,date_realized,experience_start_date,experience_end_date) values (?,?,?,?,?,?,?)', [$test->data[$i]->id,$test->data[$i]->person->id,$test->data[$i]->opportunity->id,date_format($date_approved,"Y-m-d H:i:s"),date_format($date_realized,"Y-m-d H:i:s"),date_format($experience_start_date,"Y-m-d H:i:s"),date_format($experience_end_date,"Y-m-d H:i:s")]);
        DB::insert('insert ignore into ep_users (ep_id,first_name,last_name,email,home_lc_id,signup_date,dob,status) values (?, ?,?, ?,?, ?,?, ?)', [$test->data[$i]->person->id,$test->data[$i]->person->first_name,$test->data[$i]->person->last_name,$test->data[$i]->person->email,$test->data[$i]->person->home_lc->id,date_format($signup_date,"Y-m-d H:i:s"),$test->data[$i]->person->dob,$test->data[$i]->status]);


        }

    }

上面给出了获取json文件然后使用json_decode的代码,它给出了这样的答案:

array:3
 [
  "paging" => array:3 
[
    "total_items" => 216822
    "current_page" => 1
    "total_pages" => 1085
  ]
  "data" => array:200 [
    0 => array:19 [
      "id" => 3719730
      "url" => ""
      "status" => "open"
      "current_status" => "open"
      "an_signed_at" => null
      "nps_grade" => null
      "date_realized" => null
      "date_approved" => null
      "custom_status" => null
      "experience_start_date" => null
      "experience_end_date" => null
      "favourite" => null
      "person" => array:23 [ …23]
      "opportunity" => array:15 [ …15]
      "standards" => []
      "scheduled_interview" => null
      "permissions" => array:26 [ …26]
      "created_at" => "2017-12-14T10:27:06Z"
      "updated_at" => "2017-12-14T10:27:06Z"
    ]
....
    198 => array:19 [ …19]
    199 => array:19 [ …19]
  ]
  "facets" => null
]

2 个答案:

答案 0 :(得分:1)

这是因为$test是数组而不是对象。

替换所有出现的:

$test->data

$test['data']

答案 1 :(得分:0)

$test=json_decode($response,true);更改为$test=json_decode($response);$size=count($test['data'])$size=count($test->data)

当使用json_decode($ variable, true )时,返回的对象将被转换为关联数组。