Search code examples
apisymfonyrequestbasic-authenticationphp-curl

how to make external API call with basic authentification from Symfony 4 controller?


As mentioned in the question I am trying to call this Adyen API with specific authentification credentials and passing JSON object, Using Curl it is done like this.

curl https://pal-test.adyen.com/pal/servlet/Recurring/v68/disable \
-U "ws@Company.login":"Pa$$W0rd" \
-H "Content-Type: application/json" \
-d '{
  "merchantAccount": "exampleMerchantAccount",
  "shopperReference": "exampleShopperReference"
    }'

I tried to do this using HTTPClient Request:

$params = array(
        "merchantAccount" => "exampleMerchantAccount",
        "shopperReference" => "exampleShopperReference"
    );
$result = $this->client->request('POST', 'https://pal-test.adyen.com/pal/servlet/Recurring/v68/disable', $params);

But I still can't understand how to pass the basic authentification credentials.

Any help please .


Solution

  • As written in the documentation, add it to the parameters like this:

    $result = $this->client->request(
      'POST',
      'https://pal-test.adyen.com/pal/servlet/Recurring/v68/disable',
      [
       'auth_basic' => ['ws@Company.login', 'Pa$$W0rd'],
       'body' => $params,
      ]
    );