仅从json答案中获取少量数据(不需要所有其他数据)

mfuanj7w  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(311)

使用代码此代码通过api从其他服务器获取响应:

$ch = curl_init('https://apipage.com');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Token xxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$html = curl_exec($ch);
curl_close($ch);
echo $html;

结果-返回json响应一次:

Response headers:
200 success
access-control-allow-credentials: true
access-control-allow-headers: accept, origin, x-requested-with, authorization, content-type
access-control-allow-methods: OPTIONS, GET, POST, PUT, DELETE
access-control-allow-origin: *
cache-control: no-store, no-cache, must-revalidate
content-length: 1046
content-type: application/json; charset=utf-8
date: Sun, 25 Jul 2021 14:14:51 GMT
expires: Thu, 19 Nov 1981 08:52:00 GMT
pragma: no-cache
server: nginx
x-vendon-api-requests: 2

Response data:
{
    "code": 200,
    "result": {
        "id": 3075531,
        "stock_id": 184445,
        "stock_article": "Parfum 2 ml 047 women",
        "selections": [
            {
                "selection": 11,
                "label": "11",
                "price": 1,
                "pricelist": null,
                "pricelist_type": null,
                "payment_device_type": null
            }
        ],
        "type": "PRODUCT",
        "name": "Parfum 2 ml 047 women",
        "units": null,
        "amount": 10,
        "amount_max": 11,
        "amount_standart": 11,
        "amount_critical": 3,
        "refill_unit_size": 1,
        "min_refill": 1,
        "refillable": true,
        "last_purchase": 1624011420,
        "currency": "EUR",
        "recipe": [],
        "ingredient_line": null,
        "critical": false,
        "extraction_time_enabled": null,
        "min_extraction_time": null,
        "max_extraction_time": null,
        "product_price_group": null,
        "has_route_planing_expiry_date": false
    }
}

如何从所有这些答案中抓住:“姓名”:“香水2毫升047女性”“数量”:10,
只回应他们,而不是整个json回答???还有一个问题,如何不在页面加载时运行这个请求,而只在按下一些按钮后运行?当然,我不能更改第二个服务器端的代码来发送不同的响应。谢谢大家的帮助。

56lgkhnf

56lgkhnf1#

我们得到的响应是json,因此使用它:
解码json

$answer = json_decode($html, true);

而不是回音输出

$answer = json_decode($html, true);
echo 'Item:' .$answer['result']['name'].' available '.$answer['result']['amount'].'pcs.';

谢谢你的帮助。

相关问题