使用以下代码通过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
}
}
如何从所有这些答案中抓取:name": "香水2 ml 047 women">"amount" 10,
只回显它们,而不是整个json答案??第二个问题,如何运行这个请求不是在页面加载,而是在一些按钮按下之后?可以肯定的是,我不能改变代码在第二个服务器端发送不同的响应。谢谢大家的帮助。
我们得到的响应是JSON,所以使用它:
解码json
$answer = json_decode($html, true);
$answer = json_decode($html, true);
echo 'Item:' .$answer['result']['name'].' available '.$answer['result']['amount'].'pcs.';
感谢所有帮助我的人。