【问题标题】:Get Value Json RajaOngkir [closed]获取价值 Json RajaOngkir [关闭]
【发布时间】:2018-01-11 01:52:01
【问题描述】:

我发现很难从 json 的结果中获得价值,如果你有答案,请帮助我...

这是我的代码

<?php
$asal = '2';
$id_kabupaten = '3';
$kurir = 'jne';
$berat = '1000';

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "http://api.rajaongkir.com/starter/cost",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "origin=".$asal."&destination=".$id_kabupaten."&weight=".$berat."&courier=".$kurir."",
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded",
    "key: ***********"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}?>

【问题讨论】:

  • 一遍又一遍地重复相同的无用文本以绕过最低文本要求,这违反了网站指南。这也是对本网站用户的极大不尊重。如果您懒得花精力清楚地解释问题,那么尝试帮助您是不值得的。在此处发布您的下一个问题之前,请使用tour 并阅读help center 指南,尤其是How to Askminimal reproducible example。如果本网站的要求太多,您无法遵循,请随时去其他地方寻求帮助。

标签: php html json curl


【解决方案1】:

您回显结果,而不是您应该json_decode 它,然后您可以访问它的值。

您可能还想重新生成您的 API 密钥。

<?php
$asal = '2';
$id_kabupaten = '3';
$kurir = 'jne';
$berat = '1000';

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "http://api.rajaongkir.com/starter/cost",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "origin=".$asal."&destination=".$id_kabupaten."&weight=".$berat."&courier=".$kurir."",
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded",
    "key: *********************"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    exit("cURL Error #:" . $err);
}

$response = json_decode($response);

$response 现在将成为一个对象,例如:

/*
stdClass Object
(
    [rajaongkir] => stdClass Object
        (
            [query] => stdClass Object
                (
                    [origin] => 2
                    [destination] => 3
                    [weight] => 1000
                    [courier] => jne
                )

            [status] => stdClass Object
                (
                    [code] => 200
                    [description] => OK
                )

            [origin_details] => stdClass Object
                (
                    [city_id] => 2
                    [province_id] => 21
                    [province] => Nanggroe Aceh Darussalam (NAD)
                    [type] => Kabupaten
                    [city_name] => Aceh Barat Daya
                    [postal_code] => 23764
                )

            [destination_details] => stdClass Object
                (
                    [city_id] => 3
                    [province_id] => 21
                    [province] => Nanggroe Aceh Darussalam (NAD)
                    [type] => Kabupaten
                    [city_name] => Aceh Besar
                    [postal_code] => 23951
                )

            [results] => Array
                (
                    [0] => stdClass Object
                        (
                            [code] => jne
                            [name] => Jalur Nugraha Ekakurir (JNE)
                            [costs] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [service] => CTC
                                            [description] => JNE City Courier
                                            [cost] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [value] => 20000
                                                            [etd] => 2-3
                                                            [note] => 
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

) 
*/

然后你会像这样访问:

echo $response->rajaongkir->results[0]->costs[0]->cost[0]->value;

【讨论】:

  • 错误 -_- 这是我的代码:echo $response->rajaongkir->destination_details->value;
  • 上述对象中的value在哪里?试试echo $response-&gt;rajaongkir-&gt;destination_details-&gt;province;
  • 有city_id,province_id,province,type,city_name,postal_code..
  • 这是结果:
  • 你想从数据中得到什么价值?
猜你喜欢
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 2021-05-24
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多