我需要一些关于 paypal rest api 的帮助。
我使用 guzzle 作为 http 客户端来使用 paypal api
当我在命令行中使用 curl 尝试 paypal 示例时,它确实有效
但是当我想用 guzzle 重现它时,我总是从 paypal 得到 Internal 500 ERROR..
这是官方文档中的 paypal curl 示例(请在此处查看 https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/):
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "clientId:clientSecret" \
-d "grant_type=client_credentials"
这是我的 Guzzle 代码:
/**
* Etape 1 récuperer un access token
*/
$authResponse = $client->get("https://api.sandbox.paypal.com/v1/oauth2/token", [
'auth' => [$apiClientId, $apiClientSecret, 'basic'],
'body' => ['grant_type' => 'client_credentials'],
'headers' => [
'Accept-Language' => 'en_US',
'Accept' => 'application/json'
]
]);
echo $authResponse->getBody();
我尝试过 auth basic、digest,但到目前为止都没有用。
感谢您对此的任何帮助!
最佳答案
这是使用 guzzlehttp
$uri = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$clientId = \\Your client_id which you got on sign up;
$secret = \\Your secret which you got on sign up;
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'headers' =>
[
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => 'grant_type=client_credentials',
'auth' => [$clientId, $secret, 'basic']
]
);
$data = json_decode($response->getBody(), true);
$access_token = $data['access_token'];
关于curl - 将 paypal curl get access token 请求转换为 guzzle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24951909/