Skip to main content
  • Create new account
  • Reset your password
张文涛的个人博客
致力于提供Drupal开发,实践等方面的精品资讯

Main navigation

  • 首页
  • 关于
  • Glossary
  • 分享

Breadcrumb

  • Home
  • oauth2 server模块的services接口调用代码片段
By admin | 2:46 AM CST, Sat November 21, 2015
define("TOKEN_URL", 'http://example.com/oauth2/token');
define("UPDATE_URL", 'http://example.com/api/order/update-status');
//第一步:调用token接口,获取token。
$data = array(
    'grant_type' => 'client_credentials',
    'client_id' => 'your_client_id',
    'client_secret' => 'your_client_secret',
    'scope' => 'basic', //这里可以根据设置好的scope灵活填写。
);
$fields_string = '';
foreach ($data as $key => $value) {
    $fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TOKEN_URL);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$resp = json_decode($result, TRUE);
print_r($resp);
$access_token = $resp['access_token'];

//第二步:使用上一部获取的token,进行接口请求。
$fields = array(
  'order_id' => 888,
  'status' => 'pending' 
);
$header = array(
  'Content-Type: application/json',
  'Authorization:' . $resp['token_type'] . ' ' . $resp['access_token'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, UPDATE_URL);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$decode = json_decode($result, TRUE);
curl_close($ch);
print_r($decode);


Glossary

  • May 2023 (7)
  • March 2023 (1)
  • October 2022 (1)
  • October 2021 (2)
  • August 2021 (4)
  • June 2021 (2)
  • May 2021 (1)
  • April 2021 (2)
  • November 2020 (1)
  • June 2020 (2)
  • December 2019 (2)
  • November 2019 (1)
  • August 2019 (1)
  • February 2018 (1)
  • October 2017 (1)
  • September 2017 (1)
  • July 2017 (1)
  • June 2017 (2)
  • May 2017 (1)
  • March 2017 (3)
  • December 2016 (2)
  • November 2016 (4)
  • October 2016 (3)
  • June 2016 (1)
  • December 2015 (1)
  • November 2015 (6)
  • October 2015 (2)
  • September 2015 (1)
  • August 2015 (1)
  • July 2015 (1)
  • June 2015 (1)
  • May 2015 (1)
  • March 2015 (1)
  • January 2015 (1)
  • December 2014 (3)
  • October 2014 (5)
  • September 2014 (3)
  • August 2014 (1)
  • July 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (2)
  • March 2014 (2)
  • February 2014 (2)
  • January 2014 (4)
  • December 2013 (1)
  • March 2013 (2)
  • February 2013 (2)
  • December 2012 (2)
  • November 2012 (4)
  • October 2012 (1)
  • September 2012 (6)
  • August 2012 (15)
  • July 2012 (5)
  • June 2012 (13)
  • May 2012 (9)
  • April 2012 (8)
  • March 2012 (9)
  • February 2012 (3)
  • January 2012 (1)
  • December 2011 (1)
  • November 2011 (6)
  • October 2011 (2)
  • September 2011 (17)
  • August 2011 (4)
  • July 2011 (4)
  • June 2011 (5)
  • May 2011 (4)

Copyright © 2025

苏ICP备14044171号-1