2

I am trying to implement VISA Developer Foreign Exchange API using CURL but when i send a request, i get Authentication Error message. I am testing the API locally and here is my implementation to it.

$data_string = $_POST;                                                                                   
$ch = curl_init('https://sandbox.api.visa.com/forexrates/v1/foreignexchangerates');   
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Accept:application/json", "Authorization:".base64_encode("usernamestring:passwordstring")));
curl_setopt($ch, CURLOPT_URL, "https://sandbox.api.visa.com/forexrates/v1/foreignexchangerates");  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_string));
curl_setopt($ch, CURLOPT_POST, 1); 
$results = curl_exec($ch);

They also generate a .pem certifcate which i am not sure if i have to use for Foreign Exchnage API request but can you please have a look and see if i am doing anything wrong?

Braiam
  • 1
  • 11
  • 47
  • 78
Osama Yawar
  • 49
  • 1
  • 9

2 Answers2

1

This api is mutual auth and to call the api you need to create an app for this api on developer platform and then need to pass your userid password in authorization header. userid and password are specific to app you created and can be seen in your app details page.

Apart from authorization header you need to send key and cert files also. key file will be created during app creation and will be downloaded to your system.

Use sample code provided for PHP by platform. To get access to sample code you need to create an app for this api.

Braiam
  • 1
  • 11
  • 47
  • 78
0

You need to pass the Private Key (.pem file downloaded at time of creating project) and cert key (.pem file in credentials) in userid/password section.

Use the sample code provided in PHP or try below code.

$userId = ""; /*Your user id*/
$password = ""; /*Your password*/
$postBody = array(); /*Your POST body*/
$authString = $userId.':'.$password;
$authStringBytes = utf8_encode($authString);
$authloginString = base64_encode($authStringBytes);
$authHeader= "Authorization:Basic ".$authloginString;
$header = (array("Accept: application/json", "Content-Type: application/json", $authHeader));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, "https://sandbox.api.visa.com/forexrates/v1/foreignexchangerates");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postBody));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLCERT, 'PATH_TO_CERT_FILE');
curl_setopt($ch, CURLOPT_SSLKEY, 'PATH_TO_PRIVATE_KEY_FILE');
$results = curl_exec($ch);
curl_close($ch);
print_r(json_decode($results));
Captain Sparrow
  • 1,114
  • 17
  • 26