Php curl post in api - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, March 21, 2022

Php curl post in api

i need to receive a token to have access to an api using php and curl, this is the example of the requested curl command.

 curl -v --key private-key.pem --cert certificate.crt --location --request POST
 'https://api-homol.sicoob.com.br/cooperado/pix/token' --header 'Content-Type:
  application/x-www-form-urlencoded' --data-urlencode 'grant_type=client_credentials' 
  --data-urlencode 'client_id=xxxxxxxxxxxxxxxxxxxx' --data-urlencode
 'client_secret=xxxxxxxxxxxxxxxxxx' --data-urlencode 'scope=cob.read'

This is my function that by far is close to working, I need to attach the public and private keys and something else that I don't understand.

  //ENDPOINT COMPLETO
  $endpoint = $this->baseUrl.'/token';

  //HEADERS
  $headers = [
    'application/x-www-form-urlencoded'
  ];

  //CORPO DA REQUISIÇÃO
  $request = [
    'grant_type' => 'client_credentials',
    'client_id' => 'dda05e79c9fe2957d08a553596a1c7fe',
    'client_secret' => '18c00f72bbbeea0b2283ac6bae8d5f0a',
    'scope' => 'cob.read'
  ];

  //CONFIGURAÇÃO DO CURL
  $curl = curl_init();
  curl_setopt_array($curl,[
    CURLOPT_URL            => $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST  => 'POST',
    CURLOPT_POSTFIELDS     => http_build_query($request),
    CURLOPT_SSLCERT        => $this->certificate,
    CURLOPT_SSLCERTPASSWD  => '',
    CURLOPT_HTTPHEADER     => $headers
  ]);

  //EXECUTA O CURL
  $response = curl_exec($curl);
  curl_close($curl);

  //RESPONSE EM ARRAY
  $responseArray = json_decode($response,true);


source https://stackoverflow.com/questions/71549525/php-curl-post-in-api

No comments:

Post a Comment