LinkedIn API file_get_contents超时

时间:2015-05-02 21:55:11

标签: php linkedin

我目前使用file_get_contents()来调用LinkedIn身份验证API。

我成功拨打了/uas/oauth2/authorization,但当我使用/uas/oauth2/accessToken致电file_get_contents()时,它就超时了。

奇怪的是它在我的本地主机上完美运行。

我确保allow_url_fopen已开启并设法通过file_get_contents()打开google.com。

正如你可能想象的那样,它让我疯狂地试图调试它(并修复它)。

你们有没有对这种情况有什么建议?

1 个答案:

答案 0 :(得分:3)

问题是因为/uas/oauth2/accessToken需要POST类型方法,file_get_contents总是使用GET。考虑切换到curl,下面提供了两种呼叫的方法。

此信息在documentation

中提供
  

两个电话的变量

$apiKey = '';
$state = '';
$scope = '';
$redirectUri = '';
  

/ UAS /的oauth2 /授权

$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);