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

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

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

  1. $ch = curl_init('https://apipage.com');
  2. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Token xxxxxxxx'));
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  4. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  5. curl_setopt($ch, CURLOPT_HEADER, false);
  6. $html = curl_exec($ch);
  7. curl_close($ch);
  8. echo $html;

结果-返回json响应一次:

  1. Response headers:
  2. 200 success
  3. access-control-allow-credentials: true
  4. access-control-allow-headers: accept, origin, x-requested-with, authorization, content-type
  5. access-control-allow-methods: OPTIONS, GET, POST, PUT, DELETE
  6. access-control-allow-origin: *
  7. cache-control: no-store, no-cache, must-revalidate
  8. content-length: 1046
  9. content-type: application/json; charset=utf-8
  10. date: Sun, 25 Jul 2021 14:14:51 GMT
  11. expires: Thu, 19 Nov 1981 08:52:00 GMT
  12. pragma: no-cache
  13. server: nginx
  14. x-vendon-api-requests: 2
  15. Response data:
  16. {
  17. "code": 200,
  18. "result": {
  19. "id": 3075531,
  20. "stock_id": 184445,
  21. "stock_article": "Parfum 2 ml 047 women",
  22. "selections": [
  23. {
  24. "selection": 11,
  25. "label": "11",
  26. "price": 1,
  27. "pricelist": null,
  28. "pricelist_type": null,
  29. "payment_device_type": null
  30. }
  31. ],
  32. "type": "PRODUCT",
  33. "name": "Parfum 2 ml 047 women",
  34. "units": null,
  35. "amount": 10,
  36. "amount_max": 11,
  37. "amount_standart": 11,
  38. "amount_critical": 3,
  39. "refill_unit_size": 1,
  40. "min_refill": 1,
  41. "refillable": true,
  42. "last_purchase": 1624011420,
  43. "currency": "EUR",
  44. "recipe": [],
  45. "ingredient_line": null,
  46. "critical": false,
  47. "extraction_time_enabled": null,
  48. "min_extraction_time": null,
  49. "max_extraction_time": null,
  50. "product_price_group": null,
  51. "has_route_planing_expiry_date": false
  52. }
  53. }

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

56lgkhnf

56lgkhnf1#

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

  1. $answer = json_decode($html, true);

而不是回音输出

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

谢谢你的帮助。

相关问题