【问题标题】:How to get data json with https not http in php codeigniter如何在php codeigniter中使用https而不是http获取数据json
【发布时间】:2019-04-06 03:36:34
【问题描述】:

来自带有 https 的 url 的 json 数据的结果是混乱的,并且像错误字符串字符一样使用了 file_get 内容,如下面的代码所示

<?php

// header('Content-Type : application/x-www-form-urlencoded');
// header("Content-Type: text/html");

$url = "https://sirup.lkpp.go.id/sirup/servicecdn/paketpenyediapersatkertampil ?idSatker=95966&tahunAnggaran=2018";

// $url="http://jsonplaceholder.typicode.com/posts/";
// $url="http://localhost/appTestBengkulu/restAPI/aksesdata/";

$get_url = file_get_contents($url);
$datajson = json_decode($get_url);
var_dump($get_url);

// var_dump($datajson);

$data_array = array(
    'datalist' => $datajson
);

// var_dump($data_array);

$this->load->view('vdatajson', $data_array);

with:file_get 内容网址 https://drive.google.com/open?id=1tGYU4lwAHKQMs8bN5Z9ns-45c4IZEZe_

与邮递员: https://drive.google.com/file/d/1-G_2LBT53vq-jyHWahBQyAdT203O86CY/view?usp=sharing

我使用了一些类似内容类型 utf8 或类似的建议,但结果是一样的 数据可以像数组数据 json 一样读取以供查看

【问题讨论】:

  • 如何在视图中打印 vdatajson?
  • 可能是你贴代码的时候弄错了,但是url中?idSatker之前有空格

标签: php json codeigniter


【解决方案1】:

你有两个问题,首先在网址中paketpenyediapersatkertampil?idSatker之间有空格,删除它。
二、返回的数据是gzip格式的,可​​以先用gzdecode函数解压,然后再json_decode它:

<?php

// header('Content-Type : application/x-www-form-urlencoded');
// header("Content-Type: text/html");

$url = "https://sirup.lkpp.go.id/sirup/servicecdn/paketpenyediapersatkertampil?idSatker=95966&tahunAnggaran=2018";

// $url="http://jsonplaceholder.typicode.com/posts/";
// $url="http://localhost/appTestBengkulu/restAPI/aksesdata/";

$get_url = file_get_contents($url);
$datajson = json_decode(gzdecode($get_url));
// var_dump($get_url);

// var_dump($datajson);

$data_array = array(
    'datalist' => $datajson
);

var_dump($data_array);

$this->load->view('vdatajson', $data_array);

【讨论】:

    【解决方案2】:

    cURL 效果很好

    这是一个有效的 sn-p。

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://sirup.lkpp.go.id/sirup/servicecdn/paketpenyediapersatkertampil?idSatker=95966&tahunAnggaran=2018",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      相关资源
      最近更新 更多