我需要连接到客户的 REST API。第一步是获取 OAuth 2.0 访问令牌。我从客户那里得到以下信息:
client_id)client_secret)password我在http://tutorialspage.com/simple-oauth2-example-using-php-curl/找到了代码 并将其更改为仅用于测试:
private function getToken() {
$curl = curl_init();
$params = array(
CURLOPT_URL => self::API_TOKEN_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"accept: *",
"accept-encoding: gzip, deflate",
),
CURLOPT_POSTFIELDS => array(
'username' => urlencode('example@swagger.foo'),
'password' => urlencode('123 and 4'),
'grant_type' => 'password'
) …Run Code Online (Sandbox Code Playgroud)