请求HTTP POST cURL

时间:2015-03-11 15:22:30

标签: php http post curl

http发布请求有问题。参数是正确的,但我有一个错误。

这是我的代码

Route::get('login/linkedin', function()
{
    $lk_credentials = Config::get('linkedin.public0');
    $provider = new LinkedIn($lk_credentials);
    if(!Input::has('code')){
        //exit('debug');
        $provider->authorize();
    }else{
        try{
            $code = Input::get('code');
            if(strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with LinkedIn');
            $t = $provider->getAccessToken('authorization_code', array('code' => $code));
        }catch(Exception $e){
            return 'Unable to get access token';
        }
        try{
            $userDetails = $provider->getUserDetails($t);
            $resource = '/v1/people/~:(id,emailAddress,firstName,lastName,pictureUrl,dateOfBirth,location)';
            $params = array(
                'oauth2_access_token' => $t->accessToken,
                'format' => 'json',
            );
            Session::put('oauth2_access_token', $t->accessToken);session(['oauth2_access_token' => $t->accessToken]);
            $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
            $context = stream_context_create(array('http' => array('method' => 'GET')));
            $response = file_get_contents($url, false, $context);
            $data = json_decode($response);
            Session::put('data', $data);session(['data' => $data]);
        }catch(Exception $e){
            return 'Unable to get user details';
        }
        try{
            if(count($_POST) > 0){
                print_r($_POST);
                exit();
            }
            $params = array(
                'grant_type' => 'authorization_code',
                'code' => Session::get('oauth2_access_token'),
                'redirect_uri' => url('login/linkedin'),
                'client_id' => '************',
                'client_secret' => '***************',
            );
            $c = curl_init();
            curl_setopt($c, CURLOPT_URL, 'http://www.linkedin.com/uas/oauth2/accessToken?');
            curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($c, CURLOPT_POST,true);
            curl_setopt($c, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($c, CURLOPT_POSTFIELDS,$params);
            curl_setopt($c, CURLOPT_HEADER, false);
            curl_setopt($c, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/x-www-form-urlencoded',
                )
            );
            $output = curl_exec($c);
            if($output === false){
                trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);
                exit('Erreur');
            }
            else{
                var_dump($output);
                exit('Affiche');
            }
            curl_close($c);
            return redirect('/')->with('data',$data);
        }catch(Exception $e){
            return 'Unable to get Request Token';
        }
    }
});

这是我的错误:

{"error_description":"missing required parameters,
includes an invalid parameter value, parameter more than once.
: Unable to retrieve access token : authorization code not found",
"error":"invalid_request"}

1 个答案:

答案 0 :(得分:0)

在构建$params数组时,请记住该url必须是urlencoded,因此如果您的url函数没有这样做,请将此行更改为:

'redirect_uri' => urlencode(url('login/linkedin'))

然后像这样制作帖子字符串:

  $params = http_build_query($params);

并移除?末尾的CURLOPT_URL

此外,您应该加入CURLOPT_USERAGENT