简体   繁体   中英

How to redirect to URL received from JSON response using PHP

I using process.php to post to an API and receive the following response

{ "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 
}

Now after receiving the response, I want to redirect the page to the longurl value in the response json, How should I do it?

use json_decode to parse json in array or object and then use header function to redirect it

$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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM