将paypal curl获取访问令牌请求转换为guzzle
我需要一些关于贝宝RESTAPI的帮助 我使用guzzle作为http客户端来使用PayPalAPI 当我使用curl在命令行中尝试paypal示例时,它确实有效 但当我想用guzzle复制它时,我总是从paypal那里得到内部500错误 下面是官方文档中的paypal curl示例(请检查此处): 这是我的狂饮代码:将paypal curl获取访问令牌请求转换为guzzle,curl,paypal,internal,guzzle,Curl,Paypal,Internal,Guzzle,我需要一些关于贝宝RESTAPI的帮助 我使用guzzle作为http客户端来使用PayPalAPI 当我使用curl在命令行中尝试paypal示例时,它确实有效 但当我想用guzzle复制它时,我总是从paypal那里得到内部500错误 下面是官方文档中的paypal curl示例(请检查此处): 这是我的狂饮代码: /** * Etape 1 récuperer un access token */ $authResponse = $client->get("https://api
/**
* 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,但到目前为止都没有成功
谢谢你在这方面的帮助 您的问题在第一行
$authResponse = $client->get
这应该是一个帖子
$authResponse = $client->post
我有一点类似的问题
编辑:我还喜欢,如果您希望对请求的主体使用JSON,或者使用JSON_encode,或者用JSON替换主体,guzzle将处理其余部分。在以前的代码中
$authResponse = $client->post("https://api.sandbox.paypal.com/v1/oauth2/token", [
'auth' => [$apiClientId, $apiClientSecret, 'basic'],
'json' => ['grant_type' => 'client_credentials'],
'headers' => [
'Accept-Language' => 'en_US',
'Accept' => 'application/json'
]
])) 我从“Filippo De Santis”代码中得到帮助。他已经用guzzle 3.9.1实现了一个非常好的PayPal REST API客户端,你可以在上面找到它,你可以直接使用他的代码 但如果你还想用口香糖。所以我从他的代码中得到帮助,并使用下面的代码,它对我来说非常适合
$client = new \Guzzle\Http\Client();
$clientId = "xxxxxxxxx";
$secret = "yyyyyyyyyy";
$request = $client->createRequest(
'POST',
'https://api.sandbox.paypal.com/v1/oauth2/token',
array(
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/x-www-form-urlencoded'
),
'grant_type=client_credentials',
array(
'auth' => array($clientId, $secret),
)
);
$response = $client->send($request);
$data = json_decode($response->getBody(), true);
var_dump($data);
请注意,我使用的是guzzle 3.9.1这是在使用guzzle http
$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'];
你有没有想过?我也有同样的问题!需要注意的是,我认为您的
'body'
应该是'query'
,但对我来说,这并没有什么不同。我最终使用了一个自定义的curl代码。但我想问题是我们应该使用guzzle扩展来通过承载头处理oauth身份验证。签出此链接具有“Content Type”=>“application/x-www-form-urlencoded”的部分实际上对于live server是必不可少的
$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'];