2011-04-15 75 views
2

結束時,我有這段代碼:追加JSON對象URL

 
     //execute post (the result will be something like {"result":1,"error":"","id":"4da77536a2338","lockcode":"4af15b3725eb5cf3641d96f6c7a3b71d"}) 
     $result = curl_exec($ch); 
     $response = json_decode($result); 

     //close connection 
     curl_close($ch); 

     $imageHref = 'http://my.site.com/render?id=' . $response['id']; 

但我不能將ID添加到URL,因爲某些原因結束。有任何想法嗎?

+0

當你說「但我不能出於某種原因」你是什麼意思?索引不存在嗎?字符串是否不連接?無論如何,你可能會發現'print_r($ response)'有幫助,並確保'$ response'包含你認爲它的作用。 – Mala 2011-04-15 22:38:46

+0

它的確如此。它正是我在評論中所說的。我想獲得該id並將其追加到url的末尾。 – 2011-04-15 22:39:50

+0

stringify json然後URLencode它 – 2011-04-15 22:43:11

回答

5

它的becase json解碼不會給你一個數組,而是一個對象。相反:

$imageHref = 'http://my.site.com/render?id=' . $response->id; 
+0

@Gary它實際上是另一種方式。默認情況下它返回obj。設置爲true返回assoc。陣列。 – Jage 2011-04-15 22:45:36

+0

@Gary我認爲你已經倒退了,[至少對於當前版本的PHP](http://php.net/manual/en/function.json-decode.php) – sdleihssirhc 2011-04-15 22:45:38

+0

@Gary Green:看一看在http://php.net/manual/en/function.json-decode.php – 2011-04-15 22:47:14

3

你的代碼失敗的原因是因爲你試圖使用一個對象,就好像它是一個數組。用你的最後一行代替:

$imageHref = 'http://my.site.com/render?id=' . $response->id; 
+0

的例子中,他被jage忍者忍者;) – Mala 2011-04-15 22:43:39

+0

他忍住了我的評論1秒! – sdleihssirhc 2011-04-15 22:46:07

+0

awww shucks,夥計們。 – Jage 2011-04-15 22:48:32