2016-03-01 63 views
-2

我已經提出了一個API請求的函數。 (functions.php)我已經把這個函數調用到一個名爲(results.php)的文件中,該函數從搜索輸入中選擇ID從數據庫中取出緯度和長度, 我希望能夠爲每個數據執行一次回。爲每個對象存儲lat和long到一個數組中。將json數據粘貼到json查看器中以更好地查看它。整個事情從數據庫連接工作到數據輸出我只是無法弄清楚如何將這個JSON數據存儲到一個變量,就像我說的把每個對象的緯度和長度放到一個數組中如何將API請求的json數據存儲到數組中

FUNCTIONS.PHP

function locate($id, $connect) { //connect is coming from connection.php 


// Using prepared Statements means that SQL injection is not possible. Selects  ID from search input takes lat and long. 

    if ($stmt = $connect->prepare("SELECT Lat, Lng FROM List WHERE ID = ? LIMIT 1")or die(mysql_error())) { 
    $stmt->bind_param('i', $id); // Bind "$id" to parameter. 
    $stmt->execute(); // Execute the prepared query. 
    $stmt->store_result(); 
    $stmt->bind_result($lat, $lng); // get variables from result. 
    $stmt->fetch(); 

    if($stmt->num_rows == 1) // If the id exists 
    { 
    // Point to where you downloaded the phar 
    include('httpful.phar'); 

    // Api request! 
    $response = \Httpful\Request::get('https://data.police.uk/api/crimes-street/all-crime?lat='.$lat.'&lng='.$lng)->send(); 
    echo $response; 

    } 

    else 
    { 
    // No lat & long exists. 
    echo 'noUser'; 
    return false; 
    } 

} }

RESULTS.PHP

$id=$_POST["hiddenField"]; //ID equals the hidden field post made from the search university form 
locate($id, $connect); //function from functions.PHP 

JSON數據

[{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.629543","street":{"id":1199924,"name":"On or near Parking Area"},"longitude":"-0.744038"},"context":"","outcome_status":null,"persistent_id":"1504b407304004387c9ad979acce910041bab568af1e833045a6deb157c621ae","id":45880896,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.632071","street":{"id":1199912,"name":"On or near Bridge Street"},"longitude":"-0.756033"},"context":"","outcome_status":null,"persistent_id":"edc5f6be254e489548aad05aa5a92a3d1976fa668658940bb760aefb82788b0f","id":45882456,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.629583","street":{"id":1200038,"name":"On or near Bushey Close"},"longitude":"-0.773352"},"context":"","outcome_status":null,"persistent_id":"65c9fe3cb702f5bf7c147700262595e15ce1827d7484271dedef06444789447c","id":45880856,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.632071","street":{"id":1199912,"name":"On or near Bridge Street"},"longitude":"-0.756033"},"context":"","outcome_status":null,"persistent_id":"a604dbb1811a70e75709b01b492d19290a42fdaacaac1124f6c5b4a588b32d74","id":45882455,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.628932","street":{"id":1200020,"name":"On or near Sabina 

回答

1

See json_decode()

//Input (json) string/array 
$string = '[{"Foo":"bar","123":456}]'; 

//Convert to PHP array 
$array = json_decode($string, true): 

//Output to check 
print_r($array); 

上面會輸出:

Array 
(
    [0] => Array 
     (
      [Foo] => bar 
      [123] => 456 
     ) 
) 
+0

這是有道理的啊,但我不知道在哪裏可以把這個代碼和我的投入他們不是來自API的手冊。 Theres沒有爲每個 – LukeJamesLiam

相關問題