如何使用PHP重定向到从JSON响应接收的URL

时间:2016-04-06 05:06:47

标签: php

我使用process.php发布到API并收到以下回复

{ "payment_request": 
  { 
  "id": "554efa8b701c4021be7310b8916cc30b", 
  "phone": null, 
  "email": null, 
  "buyer_name": null, 
  "amount": "12", 
  "purpose": "product-sleek", 
  "status": "Pending", 
  "send_sms": false, 
  "send_email": false, 
  "sms_status": null, 
  "email_status": null, 
  "shorturl": null, 
  "longurl": "https://www.example.com/@user/554efa8b701c4021be7310b8916cc30b", 
  "redirect_url": "https://example.com/order-received/", 
  "webhook": null,
  "created_at": "2016-04-06T05:01:04.042Z",
  "modified_at": "2016-04-06T05:01:04.042Z", 
  "allow_repeated_payments": false 
  }, 
"success": true 
}

现在收到回复后,我想将页面重定向到响应json中的longurl值,我应该怎么做?

1 个答案:

答案 0 :(得分:1)

使用json_decode解析arrayobject中的json,然后使用header函数重定向

$json = '{ "payment_request":
  {
  "id": "554efa8b701c4021be7310b8916cc30b",
  "phone": null,
  "email": null,
  "buyer_name": null,
  "amount": "12",
  "purpose": "product-sleek",
  "status": "Pending",
  "send_sms": false,
  "send_email": false,
  "sms_status": null,
  "email_status": null,
  "shorturl": null,
  "longurl": "https://www.example.com/@user/554efa8b701c4021be7310b8916cc30b",
  "redirect_url": "https://example.com/order-received/",
  "webhook": null,
  "created_at": "2016-04-06T05:01:04.042Z",
  "modified_at": "2016-04-06T05:01:04.042Z",
  "allow_repeated_payments": false
  },
"success": true
}';

$json_array = json_decode($json, true);

$longurl = $json_array['payment_request']['longurl'];

header("Location: $longurl");