Shopify oauth在尝试使用PHP中的curl获取访问令牌时返回false

时间:2016-02-05 20:50:16

标签: php curl oauth-2.0 shopify

我面临同样的问题:[https://stackoverflow.com/questions/32516932/shopify-oauth-returns-false-while-trying-to-fetch-the-access-token-using-curl][1]

返回false。但我使用了正确的client_id,secrect_id和代码,

代码示例:

$post = [
  'client_id' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'client_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'code'   =>  'xxxxxxxxxxxxxxxxxxxx'
];

$ch = curl_init();

curl_setopt($ch,    
CURLOPT_URL,"https://dummy.myshopify.com/admin/oauth/access_token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

如果有人知道这个问题,请尽早回答。

2 个答案:

答案 0 :(得分:1)

找到解决方案。 $ post应该是数组之类的数组(" client_id" => $ client_id," client_secret" => $ client_secret,"代码" => $ code) ;

它为我工作。

答案 1 :(得分:0)

$client_secret = 'SECRET_KEY';
$api_key = 'API_KEY';
$code = $_GET['code'];//return from callback url params after successfully install app.
$fields_post = "client_id=$api_key&client_secret=$client_secret&code=".$code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://{shop_url}/admin/oauth/access_token");
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_post);
curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
$xmlResponse = curl_exec ($ch);
$result = json_decode($xmlResponse);
echo $result->access_token;