php - Connect With PayPal : Get status 400 (Bad Request)

I am facing 400 bad request response from PayPal during getting access token based on access code. I go through below reference link https://developer.paypal.com/docs/connect-with-paypal/integrate/#1-create-the-app
I follow 5 steps perfectly but getting the issue with step no. 6. See my code below:
Request Part:
$curl = curl_init();
$base_token = "{client_id:secret}";
$data = array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
'grant_type' => 'authorization_code',
'code' => '{authorization_code}'
),
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ".$base_token,
),
);
curl_setopt_array($curl,$data );
$response = curl_exec($curl);
curl_close($curl);
Response Part:
stdClass Object (
[name] => Bad Request
[debug_id] => 5837077aa8787
[message] => java.lang.IllegalArgumentException
[details] => Array
(
)
)
Answer
Solution:
Passing an array to CURLOPT_POSTFIELDS will set the content type tomultipart/form-data
. This is wrong.
You need to pass a query string to CURLOPT_POSTFIELDS.
CURLOPT_POSTFIELDS => http_build_query(array(
'grant_type' => 'authorization_code',
'code' => '{authorization_code}'
)),
Answer
Solution:
Resolve issue see my below code:
$base_token = '{client_id:secret}';
$curl = curl_init();
$data = array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => http_build_query(array(
'grant_type'=>'authorization_code',
'code'=> $request->code,
)),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
),
CURLOPT_USERPWD => $base_token
);
curl_setopt_array($curl, $data);
$response = curl_exec($curl);
curl_close($curl);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illuminate\http\exceptions\posttoolargeexception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.