2011-04-15 79 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