Invalid parameter type when using CURL with PHP

Asked

Viewed 273 times

1

Good staff make a request in the pay.me API for me to recover some credit card data. When I use directly by the terminal with this code, I receive JSON correctly with all data requested:

curl -X GET https://api.pagar.me/1/transactions/ID_transacao -H 'content-type: application/json' -d '{
     "api_key": "CHAVE_API"
 }'

But when I try to move to php I don’t know what I’m doing wrong, as I get the following error that my parameters are incorrect from paying.

{"errors":[{"type":"invalid_parameter","parameter_name":"api_key","message":"api_key está faltando"}],"url":"/transactions/ID_transacao","method":"get"}

My PHP code is as follows:

$date = array(
    "api_key" => "CHAVE_API"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, "https://api.pagar.me/1/transactions/ID_transacao");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, 'content-type: application/json');
curl_setopt($ch, CURLOPT_POSTFIELDS, $date);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

What would be the correct way to pass this parameter?

Pay API link.me: https://docs.pagar.me/reference

NOTE: I have to use Curl to make this request, I cannot use their API for Php.

  • 1

    It seems to me that the problem is this configuration "curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');", but api_key is sent by POST, take a look at the documentation in https://www.php.net/manual/en/function.curl-setopt.php.

1 answer

2


According to the Pay.ME documentation, the request of this type would be by GET (Returning a transaction), as described in this link:

https://docs.pagar.me/reference#returning-a-transaction

Try to make the call as follows:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.pagar.me/1/transactions/1835855');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n    \"api_key\": \"SUA_API_KEY\"\n}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

  • That’s right! I was having a hard time putting together POSTFIELD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.