如何在 php codeigniter 中使用 https 而不是 http 获取数据 json


来自

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);

包含: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 一样读取以供查看

您有两个问题,首先在 url 中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);

cURL 运行良好

这是一个工作片段。

$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;
}

最新更新