問題是因爲/uas/oauth2/accessToken
需要POST類型的方法,file_get_contents總是使用GET。考慮切換到捲曲,下面提供了您的兩個呼叫的方法。
此信息可內documentation
Variables for both calls
$apiKey = '';
$state = '';
$scope = '';
$redirectUri = '';
/uas/oauth2/authorization
$postData = http_build_query(
[
'response_type' => 'code',
'client_id' => $apiKey,
'scope' => $scope
'state' => $state,
'redirect_uri' => $redirectUri
]
);
$ch = curl_init();
$endpoint = sprintf('%s?%s', 'https://www.linkedin.com/uas/oauth2/authorization', $postData);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
/uas/oauth2/accessToken
$postData = http_build_query(
[
'grant_type' => 'authorization_code',
'client_id' => $apiKey,
'scope' => $scope
'state' => $state,
'redirect_uri' => $redirectUri
]
);
$ch = curl_init();
$endpoint = 'https://www.linkedin.com/uas/oauth2/accessToken';
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
謝謝你爲這個!對此,我真的非常感激。 我試過你的方法,但是我得到了輸出''{「error_description」:「缺少必需的參數,包含一個無效的參數值,多次參數:client_id」,「error」:「invalid_request」}'' 然後,我將帖子字段移到端點,它在我的本地主機上工作,但它在我的服務器上引發超時錯誤。 任何想法,爲什麼會發生這種情況? –
我也收到了這樣的迴應:'當啓用safe_mode或設置了open_basedir時'CURLOPT_FOLLOWLOCATION無法激活。你的服務器上的 –
只需將'curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);'改爲'false' – Augwa