[英]How to get data json with https not http in php codeigniter
來自url和https的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 content url https://drive.google.com/open?id=1tGYU4lwAHKQMs8bN5Z9ns-45c4IZEZe_
郵遞員: https : //drive.google.com/file/d/1-G_2LBT53vq-jyHWahBQyAdT203O86CY/view? usp =sharing
我使用了一些像內容類型utf8或類似的建議,但結果是相同的數據可以讀取像數組數據json的視圖
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;
}
你有兩個問題,首先在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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.