json_decode在php中为Web服务调用提供错误

时间:2015-01-02 12:04:09

标签: php json web-services

我正在使用PHP的Web服务,下面是代码示例

>         if ( $datas=file_get_contents($url) ) return json_decode($datas);
>         return Array('error' => "Can't open url");

我将上面函数调用的值返回给js页面。但是我没有在js中获得webservice调用的值,但总是得到错误值。但是,如果我在浏览器中打开$ url,我会得到数据(有效的json):

  

{“d”:[[5.305,428]]}

由于框架限制,我只能使用json_decode()将值返回到我的js页面。 我也试过json_decode($ datas,true);但仍然没有帮助。

我也试过在独立的php文件中使用相同的代码: 我收到此错误:当我使用json_decode($ datas)

  

捕获致命错误:无法转换类stdClass的对象   字符串在C:\ xampp \ htdocs \ webservice-demo \ clientRestTest.php上   第32行

如果我使用json_decode($ datas,true);

以下错误

  

注意:数组转换为字符串   第32行阵列上的C:\ xampp \ htdocs \ webservice-demo \ clientRestTest.php

请告诉我如何解决此问题。

1 个答案:

答案 0 :(得分:0)

$ url中是否有任何特殊字符?如果有,请先尝试urlencoding

的file_get_contents(进行urlencode($ URL));等等