【问题标题】:how to get json from this webservice http://docs.apis.is/如何从此网络服务 http://docs.apis.is/ 获取 json
【发布时间】:2014-06-21 18:43:15
【问题描述】:

我无法使用 php curl 连接到 docs.apis.is。任何人都可以帮助获得与 php 的连接并将资源保存到我可以使用的 json 文件中。

这是我到目前为止所做的

<?php
    $service_url ='http://apis.is/cinema';
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($curl);

    if ($curl_response === false) {
     $info = curl_getinfo($curl);
     curl_close($curl);
     die('Error occured during curl exec:'. var_export($info));
    }
   curl_close($curl);

   //get file and insert it into json
   $json_string = json_encode($data, JSON_PRETTY_PRINT);
   json_encode($curl, JSON_PRETTY_PRINT);
   file_put_contents('json/cinema_data.json', $curl_response);

 ?>

我似乎没有建立联系。因此,如果您能帮我解决这个问题,并让我以正确的方式使用 ajax 将 json 文本显示到我的 html 页面。

谢谢

【问题讨论】:

  • 你的服务器启用 curl 了吗?
  • 响应返回 JSON 数据,看起来不错,请检查您是否安装了 CURL。
  • 问题是拼写错误。他的代码一团糟。

标签: php ajax json curl


【解决方案1】:
...
curl_close($curl);
$json_string = json_encode(json_decode($curl_response), JSON_PRETTY_PRINT);
file_put_contents('json/cinema_data.json', $json_string);

注意JSON_PRETTY_PRINT 提高了 JSON 数据的可读性人类,但它也使文件更大。

还要注意$curl_response 已经是一个 JSON 字符串,只是不是“美化的”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多