返回

如何使用 PHP 从 JSON 中提取和访问数据?

发布时间:2022-08-05 05:36:38 299
# 后端

请帮我为什么出错?API Json PHP上的偏移量?

警告:中的字符串偏移量“data”非法

我的代码:

$nip = [234234234234243,34534534534,2343423423423];

$ch = curl_init();
foreach ($nip as $n => $value) {
    $url = "https://wsrv.bkn.go.id/api/pns/rw-diklat/$value";
    $dataArray = ['page' => 1];

    $data = http_build_query($dataArray);
    $getUrl = $url."?".$data;

    curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'DEFAULT@SECLEVEL=1');
    curl_setopt($ch, CURLOPT_URL, $getUrl);
    curl_setopt($ch, CURLOPT_TIMEOUT, 80);
    curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded',
    'Origin: http://localhost:20000',
    'Authorization: Bearer c8a5b253-bla-bla-bla'
    ),
    ));

    $response = curl_exec($ch);
    $api      = json_encode($response, true);
    if(curl_error($ch)){
        echo 'Request Error:' . curl_error($ch);
    }else{
     $api1 = json_decode($api, true);
     echo $api1['data'];
    }
     $api1['data'] = json_decode($api1['data'],true);
     mysqli_query($koneksi,"INSERT INTO tbl_riwayat_diklat(idPns,nipBaru,nipLama,latihanStrukturalId,latihanStrukturalNama,nomor,tanggal,tahun)
    VALUES ('" . $api['data']['idPns'] . "', '" . $api['data']['nipBaru'] . "', '" . $api['data']['nipLama'] . "', '" . $api['data']['latihanStrukturalId'] . "', '" . $api['data']['latihanStrukturalNama'] . "',
    '" . $api['data']['nomor'] . "', '" . $api['data']['tanggal'] . "', '" . $api['data']['tahun'] . "')");
    echo("Error description: " . mysqli_error($koneksi));
 }

curl_close($ch);

api获取响应:

{“代码”:1,“数据”:{“id”:“23423442”,“idPns”:“2342344”,“nipBaru”:“2342344”,“nipLama”:null,“latihanStrukturalId”:“2”,“latihanStrukturalNama”:“separa/ADUM/DIKLAT PIM TK.IV”,“nomor”:“10.052/DIKLATPIM TK.IV/LAN/BADAN DIKLAT PROV/2008”,“tanggal”:“27-12-20”08", &引用;“大军”&引用;2008"}]}{“代码”:1,“数据”:[]}{“代码”:1,“数据”:[{“id”:“4234234234”,“idPns”:“234234234”,“nipBaru”:“2342342”,“nipLama”:null,“latihanStrukturalId”:“2”,“latihanStrukturalNama”:“separa/ADUM/DIKLAT PIM TK.IV”,“nomor”:“5042/DIKLATPIM TK.IV/36/3604/LAN/2013”,“tanggal”:“20-03-2013”,“tahun”"&引用;2013"}]}

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像