嗨,我试图通过stackoverflow找到我的问题的答案,但似乎我找不到任何东西。这是我的问题,我目前正在使用 MVC 框架,我需要从控制器的模型中访问变量。这是我的模型:
<?php
use Guzzle\Http\Client;
class Position_model extends CI_Model{
public function get_location($location){
// Create a client and provide a base URL
$client = new Client('http://maps.googleapis.com/maps/api/geocode/json');
$request = $client->get('?address=' . $location . '&sensor=false');
// Send the request and get the response
$response = $request->send();
//decode json file to get the longitude and latitude
$json = json_decode($response->getBody(), true);
var_dump($json);
if($json["results"][0]["formatted_address"] AND $json["results"][0]["geometry"]["viewport"]["northeast"]){
$position["address"] = $json["results"][0]["formatted_address"];
$position["latitude"] = $json["results"][0]["geometry"]["viewport"]["northeast"]["lat"];
$position["longitude"] = $json["results"][0]["geometry"]["viewport"]["northeast"]["lng"];
return $position;
$code = 'success';
$content = 'LOCATION FOUND ... I AM AWESOME';
$this->output->set_status_header(201);
}else{
$code = 'error';
$content = 'OOPPS LOCATION NOT FOUND';
$this->output->set_status_header(400);
}
}
}
我需要从此类中获取 $position 以在名为 schedule 的控制器中使用,并将其附加到我尝试过的另一个名为 $data 的变量中:
$position = $this->Position_model->get_location($location)->position;
$data += $position;
请帮我 !!!!但是显然,这不起作用并给我错误,例如:未定义的位置或调用非对象属性