我正在使用 codeigniter 框架创建一个 api。
我已经成功地为各种 API 调用创建了一组响应。
public function test1($postcode){
//run a query
//get a response in an array structure
print_r(json_encode($response)); //print the json response
}
public function test2($postcode){
//run a query
//get a response in an array structure
print_r(json_encode($response)); //print the json response
}
所以当我单独运行它们时 - 它返回正常。
例如.. http://localhost/codeigniter/index.php/api/test1/sw1
我的主要问题是尝试将这些不同的 api 调用链接到一个主 api 调用中。所以像这样。
public function master($postcode){
//run a query
$response = array(
$this->test1($postcode),
$this->test2($postcode)
);
//get a response in an array structure
print_r(json_encode($response)); //print the json response
}
但是当我调用这个 master api 时 例如..http://localhost/codeigniter/index.php/api/master/sw1
我得到了一个奇怪的结果——我得到了两个 json 响应——但随后是一个空数组
{
"similar-property-avg-sold": [
[{
"average": "651164.042021172"
}]
]
} {
"avg-property-sold-growth": {
"data": [{
"year": "2011",
"average": "448696.91018672835"
}, {
"year": "2016",
"average": "651164.042021172"
}],
"difference": 145.12336217118
}
}
Array([0] => [1] => )
最佳答案
您没有返回前两个子方法的响应...因此主输出将为空...
public function test1($postcode){
//run a query
//get a response in an array structure
print_r(json_encode($response)); //print the json response
//return response
return $response;
}
public function test2($postcode){
//run a query
//get a response in an array structure
print_r(json_encode($response)); //print the json response
//return response
return $response;
}
关于php - Codeigniter - 链接多个查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39268397/