John Davidson

How to be redirected by an API in php?

0 comments
Message:


I am trying to implement payments through Chase Bank APIs (https://developer.chase.com/products/aggregation-consent/).
According to the documentation once I send a GET request I should be redirected to their website, however I tried to do the request through PHP and even if the request goes fine (Code 200) I don't get redirected to any website.


This is the php code (I tried to add some lines of code to see if I get any response, like var_dump or CURL_HEADER_SIZE):


<?php

include('global_variables.php');

$ch = curl_init();

$url = 'https://apidemo.chase.com/api/v1/solution/accounts-and-data-sharing/aggregator-oauth/v1/mobile/authorize?response_type=code&client-id=XYZAB12321VBF&redirect_uri=developer.chase.com&scope=aggregator&state=Union';

$header_auth = array(
'accept: application/json',
'playground-id-token: ' . $chToken
);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_auth);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$resp = curl_exec($ch);

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_size);
$body = substr($resp, $header_size);


if($e = curl_error($ch)){
echo $e;
} else {
$decoded = json_decode($resp);

var_dump($decoded);
}


curl_close($ch);


?>

I'm not expert in implementing APIs in PHP so if there is something I missed in the question just ask and I will provide it immediately.



My Answer:
To be redirected by an API in PHP, you can use the cURL library to make the GET request and handle the redirection. Here is an example code snippet that you can use:

php
$url = 'https://api.chase.com/redirect-url'; // Replace this with the actual API endpoint

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
curl_setopt($ch, CURLOPT_HEADER, true); // Include the header in the output

$response = curl_exec($ch);

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);

curl_close($ch);

var_dump($httpCode);
var_dump($header);
var_dump($body);


In this code snippet, we are making a GET request to the Chase Bank API endpoint and setting the necessary cURL options to follow redirects and include the header in the output. After executing the cURL request, we are extracting the HTTP status code, header, and body of the response.

Make sure to replace the `$url` variable with the actual API endpoint provided by Chase Bank. This code should handle the redirection and provide you with the necessary information from the response.

Rate this post

3 of 5 based on 7410 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us