拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 通过POST向API发送资料并在PHP中显示回应

通过POST向API发送资料并在PHP中显示回应

白鹭 - 2022-01-24 2103 0 0

我有一个小问题,我对 PHP 有点陌生,我无法作业。

我会解释。

我可以访问运输 API(包裹),它使我可以通过指南号或运输 ID 跟踪包裹,并以 JSON 格式回传带有运输状态的回应。

我制作了一个执行跟踪功能的小脚本,但我无法让它显示“主要”状态和“子事件”。

请求通过 POST 发送,回应如下所示:

{
    "status": "SUCCESS",
    "enviaya_shipment_number": "N9T30TAC",
    "carrier_tracking_number": "9878401142",
    "channel": "Api",
    "estimated_delivery_date": null,
    "expected_delivery_date": "12/01/2022",
    "pickup_date": "2021-12-30T10:39:11-06:00",
    "shipment_status": "En tránsito",
    "event_code": 151,
    "event_description": "Llegada al Centro de DHL",
    "event": "Llegada",
    "status_code": 14,
    "sub_event_code": null,
    "sub_event": null,
    "sub_event_description": null,
    "checkpoints": [
        {
            "code": "PU",
            "description": "Envío recibido",
            "date": "2022-01-03T12:12:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "MEXICO CITY-MEX"
        },
        {
            "code": "PL",
            "description": "Procesado",
            "date": "2022-01-03T14:07:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "MEXICO CITY-MEX"
        },
        {
            "code": "DF",
            "description": "Salida",
            "date": "2022-01-03T14:08:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "MEXICO CITY-MEX"
        },
        {
            "code": "AF",
            "description": "Llegada",
            "date": "2022-01-03T15:30:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "MEXICO CITY HUB-MEX"
        },
        {
            "code": "PL",
            "description": "Procesado",
            "date": "2022-01-03T18:55:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "MEXICO CITY HUB-MEX"
        },
        {
            "code": "DF",
            "description": "Salida",
            "date": "2022-01-03T19:04:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "MEXICO CITY HUB-MEX"
        },
        {
            "code": "AF",
            "description": "Llegada",
            "date": "2022-01-03T20:55:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "QUERETARO-MEX"
        },
        {
            "code": "PL",
            "description": "Procesado",
            "date": "2022-01-03T22:03:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "QUERETARO-MEX"
        },
        {
            "code": "DF",
            "description": "Salida",
            "date": "2022-01-03T22:31:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "QUERETARO-MEX"
        },
        {
            "code": "AR",
            "description": "Llegada",
            "date": "2022-01-04T01:52:00.000-06:00",
            "postal_code": null,
            "city": null,
            "country_code": null,
            "country": null,
            "comments": "MERIDA-MEX"
        }
    ]
}

所以我用 HTML 制作了一个表单供客户端发送资料,这很简单:

<form action="track.php" method="post" enctype="multipart/form-data" accept-charset="UTF-8" id="form-app">
<fieldset class="row">

<!-- Heading -->
<div class="col-xs-12">
    <h3 class="legend">Rastreo de Paquetes</h3>
</div>


<!-- Select List -->
<div class="col-xs-12">
    <div class="form-group required-control">
        <label  class="control-label" for="carrier">Paquetería</label>
        <select id="carrier" name="carrier" data-alias=""  class="form-control" required  >
            <option value="UPS" >UPS</option>
            <option value="Redpack" >Redpack</option>
            <option value="Estafeta" >Estafeta</option>
            <option value="DHL" >DHL</option>
            <option value="99 Minutos" >99 Minutos</option>
            <option value="FedEx" >FedEx</option>
            <option value="iVoy" >iVoy</option>
        </select>
    </div>
</div>

<!-- Number -->
<div class="col-xs-12">
    <div class="form-group required-control">
        <label  class="control-label" for="number">Número de guía o ID de Envío</label>
        <input type="text" id="shipment" name="shipment" value="" data-alias="" data-integer-only="true"  class="form-control"  required>
    </div>
</div>

<!-- Button -->
<div class="col-xs-12">
     <div class="form-action">
        <button type="submit" id="button_1" name="button_1" class="btn btn-primary">Enviar</button>
    </div>
</div>

这反过来通过 cURL 发送资料、接收回应和解码 JSON 发送到理论上与 API 连接的档案

<?php
include_once "key.php";
$shipment_number=$_POST["shipment"];
$carrier=$_POST["carrier"];

 //SEND REQUEST post TO api AND DECODE JSON RESPONSE
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://envios.corporativomarva.mx/api/v1/trackings");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, "carrier=".$carrier."&shipment_number=".$shipment_number);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     "api_key: ".$API_KEY
 ));
 $response = curl_exec($ch);
 curl_close($ch);
 $respuesta = json_decode($response);
 

//Show the response on HTML format with tables
echo "<table border='1'>";
echo "<tr><td>Estado del Envío</td><td>".$respuesta->shipment_status."</td></tr>";
echo "<tr><td>Evento</td><td>".$respuesta->event_description."</td></tr>";
echo "<tr><td>Guía</td><td>".$respuesta->carrier_tracking_number."</td></tr>";
echo "<tr><td>ID de Envio</td><td>".$respuesta->enviaya_shipment_number."</td></tr>";
echo "<tr><td>Fecha de envio</td><td>".$respuesta->pickup_date."</td></tr>";

//Show subevents on HTML format with tables
echo "<table border='1'>";
echo "<tr><td>Evento</td><td>Descripción</td><td>Fecha</td></tr>";
foreach ($respuesta->subevents as $subevent) {
  echo "<tr><td>".$subevent->event."</td><td>".$subevent->description."</td><td>".$subevent->date."</td></tr>";
}
echo "</table>";    

但是,显然有问题,因为它向我显示了空单元格,也就是说,它没有显示任何资料。

根据var_dump($respuesta);API 密钥不正确,但我已经验证并且没问题。

uj5u.com热心网友回复:

我设法找到了答案:

首先,我将 API 密钥作为标头传递,它必须作为另一个自变量传递,然后我设法让它向我显示表中的检查点,完整的代码是:

<?php
include_once "key.php";
$shipment_number=$_POST["shipment"];
$carrier=$_POST["carrier"];

$params = array(
  "api_key" => $API_KEY,
  "carrier" => $carrier,
  "shipment_number" => $shipment_number
);
$headers = array(
  "api_key=".$API_KEY
);
curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "https://envios.corporativomarva.mx/api/v1/trackings",
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_RETURNTRANSFER => 1
));
$response = curl_exec($ch);
curl_close($ch);

$respuesta = json_decode($response);
 

//Show the response on HTML format with tables
echo "<table border='1'>";
echo "<tr><td>Estado del Envío</td><td>".$respuesta->shipment_status."</td></tr>";
echo "<tr><td>Evento</td><td>".$respuesta->event_description."</td></tr>";
echo "<tr><td>Guía</td><td>".$respuesta->carrier_tracking_number."</td></tr>";
echo "<tr><td>ID de Envio</td><td>".$respuesta->enviaya_shipment_number."</td></tr>";
echo "<tr><td>Fecha de envio</td><td>".$respuesta->pickup_date."</td></tr>";

//Show checkpoints data in a table
echo "<tr><td colspan='2'><table border='1'>";
echo "<tr><td>Checkpoint</td><td>Fecha</td><td>Código</td><td>Descripción</td><td>Ciudad</td><td>Estado</td><td>País</td><td>Comentarios</td></tr>";
foreach($respuesta->checkpoints as $checkpoint){
  echo "<tr><td>".$checkpoint->description."</td><td>".$checkpoint->date."</td><td>".$checkpoint->code."</td><td>".$checkpoint->description."</td><td>".$checkpoint->city."</td><td>".$checkpoint->state."</td><td>".$checkpoint->country."</td><td>".$checkpoint->comments."</td></tr>";
}
echo "</table></td></tr>";
echo "</table>";

uj5u.com热心网友回复:

根据共享的代码片段,您正在迭代subevent. 但是,我找不到subevent在共享回应中呼叫的任何键

如果subevent存在并且它是串列的形式,那么 foreach 陈述句应该可以作业。

请确认来自哪里subevent

foreach ($respuesta->subevents as $subevent)

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *