2016-09-01 35 views
2

我使用codeignitor框架工作創建api。Codeignitor - 鏈接幾個查詢結果

我已經設法爲各種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 
} 

,但是當我把這個主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] =>) 

回答

2

您沒有返回前兩個子方法的響應...所以主輸出將爲空...

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; 
} 
+0

啊 - 是的 - 這是現在的解決方案 –

+0

- 因爲我仍然在構建api服務列表 - 是否有一種方法可以將它們的響應打印出來,如果它自己打印(直接調用) - 然後停止打印它出來 - 如果它從另一個函數調用? –

+0

我做了這樣的事情 - 讓我知道如果有一個更清潔的解決方案--- \t \t //如果直接打印出反應 \t \t if($ this-> uri-> segment(2)!=「 master「){ \t \t \t print_r(json_encode($ response)); \t \t} –