2013-07-01 14 views
0

捲曲JSON陣列響應我有像這樣轉相聯繫的數組

$ch = curl_init(); 
$data = 'filter=year(StartTime)' . urlencode(' eq 2013 and ') .'month(StartTime)'. urlencode(' eq 06') ; 
curl_setopt($ch, CURLOPT_URL, "http://url.com/id()/events?$".$data); 
$headers = array(
    'Accept: application/json', 
    'Content-type: application/json', 
    'X-ApiKey : XXXXXXXXXX' 
); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_HEADER, false); 

$response = curl_exec($ch); 
curl_close($ch); 
$result = json_decode($response, true); 

?> 
<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Untitled Document</title> 


</head> 

<body> 

<br><br> 
<?php 
echo "the name". $result['Name']; 
?> 

</body> 
</html> 

這是它打印的捲曲請求。

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 218 Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET- WEBSRV X-Powered-By: ARR/2.5 X-Powered-By: ASP.NET - ARR02 Date: Mon, 01 Jul 2013 02:52:31 GMT [{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}] 

the name 

我怎樣才能把它放到關聯數組中?

我已經試過這

json_decode($response,true)); 

ksort($response); 

var_dump($response); 

和似乎沒有任何工作..

我希望能夠像這樣輸出

echo $reponse['Name']; 

任何幫助?由於

+0

你提到'var_dump($ response);'「不起作用」 - 它給你什麼?你是否努力從CURL調用中獲取JSON字符串,或者努力將其解碼爲數組? – IMSoP

+0

如果我這樣做'$ response = var_dump($ response); echo $ response ['Name'];'在輸出這個'布爾(真)'似乎從CURL得到JSON字符串罰款..只是不能得到它關聯數組 – vinylDeveloper

+0

@IMSoP我認爲它有一些東西用我的返回字符串。一些值是整數和數組,它們周圍沒有引號。你知道我該如何處理這個?這可能是問題嗎? – vinylDeveloper

回答

2

默認情況下,curl_exec會將它從服務器獲得的數據輸出到標準輸出中,這樣您的$ response變量就沒有真正的響應數據。如果您想在調用curl_exec之前獲取變量中的數據,請設置CURLOPT_RETURNTRANSFER選項。

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE) 
+1

謝謝..我想到了這個,實際上已經嘗試過了,但卻無法實現。經過一番睡眠後,我發現了它。 – vinylDeveloper

7

json_decode爲您提供了一個關聯數組時you pass "true" as the second argument

$json = '[{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}]'; 

    $response = json_decode($json, true); 

    echo $response[0]["Name"]; 

給出:

Test 

編輯:

json_decode()是給你回一個數組的數組,所以你需要引用位置爲[0]的陣列在迴應中,如果你得到我。

我已經在我的例子中用$ response [0]完成了上述操作,但是看看這個例子,希望它更清晰!

$result = json_decode($json, true); 
    var_dump($result); 

給出:

array(1) { 
    [0]=> 
    array(10) { 
    ["Id"]=> 
    int(1079) 
    ["Name"]=> 
    string(5) "Test " 
    ["Status"]=> 
    int(1) 
    ["MemberId"]=> 
    int(1308) 
    ["Description"]=> 
    string(20) "This is a Test Event" 
    ["SponsoredBy"]=> 
    NULL 
    ["StartTime"]=> 
    string(19) "2013-06-30T12:00:00" 
    ["EndTime"]=> 
    string(19) "2013-06-30T23:59:00" 
    ["SearchDescription"]=> 
    NULL 
    ["Types"]=> 
    array(3) { 
     [0]=> 
     int(1) 
     [1]=> 
     int(4) 
     [2]=> 
     int(6) 
    } 
    } 
} 

的話..可以訪問陣列本身:

$result = json_decode($json, true); 

    $result = $result[0]; // let's just reassign this to get the array we want  
    var_dump($result); 

給出:

array(10) { 
    ["Id"]=> 
    int(1079) 
    ["Name"]=> 
    string(5) "Test " 
    ["Status"]=> 
    int(1) 
    ["MemberId"]=> 
    int(1308) 
    ["Description"]=> 
    string(20) "This is a Test Event" 
    ["SponsoredBy"]=> 
    NULL 
    ["StartTime"]=> 
    string(19) "2013-06-30T12:00:00" 
    ["EndTime"]=> 
    string(19) "2013-06-30T23:59:00" 
    ["SearchDescription"]=> 
    NULL 
    ["Types"]=> 
    array(3) { 
    [0]=> 
    int(1) 
    [1]=> 
    int(4) 
    [2]=> 
    int(6) 
    } 
} 

現在你可以訪問的各種元素該陣列直接:

$result = json_decode($json, true); 
    $result = $result[0]; 

    echo "Name: ". $result["Name"] . "\nID: " . $result["Id"] . "\nDescription: " . $result["Description"] . "\n"; 

現在我們回來:

Name: Test 
ID: 1079 
Description: This is a Test Event 

希望是有道理的!

+0

我沒有通過..仍輸出空白?奇怪的。如果我這樣做' $ response = var_dump($ response); echo $ response ['Name'];'在輸出這個'布爾(真)',任何想法可能意味着什麼? – vinylDeveloper

+0

你是什麼意思的「它輸出空白」?你能用完整的代碼更新你的問題嗎? – msturdy

+0

我已更新問題 – vinylDeveloper