首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LinkedIn PHP代码问题登录

问使用LinkedIn PHP代码问题登录
EN

Stack Overflow用户
提问于 2017-08-17 19:10:43
回答 1查看 74关注 0票数 1

下面的代码是由我实现的,用于从LinkedIn帐户检索特定的详细信息并将其保存到数据库中。尽管它们检索正确,但数据并未正确保存到数据库中。我不确定我的查询有什么问题。请看一下我的代码并指出问题所在。任何形式的帮助都是非常感谢的。

代码语言:javascript
复制
    <?php

    session_start();

    $client_id = "";
    $client_secret = "";
    $redirect_uri = "http://localhost:8888/Exercise/callback.php";
    $csrf_token = random_int(1111111, 9999999);
    $scopes = "r_basicprofile%20r_emailaddress";

    function curl($url, $parameters)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
        curl_setopt($ch, CURLOPT_POST, 1);
        $headers = [];
        $headers[] = "Content-Type: application/x-www-form-urlencoded";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);
        return $result;
    }

    function getCallback()
    {
      $client_id = "";
      $client_secret = "";
      $redirect_uri = "http://localhost:8888/Exercise/callback.php";
      $csrf_token = random_int(1111111, 9999999);
      $scopes = "r_basicprofile%20r_emailaddress";

        if (isset($_REQUEST['code'])) {
            $code = $_REQUEST['code'];
            $url = "https://www.linkedin.com/oauth/v2/accessToken";
            $params = [
                'client_id' => $client_id,
                'client_secret' => $client_secret,
                'redirect_uri' => $redirect_uri,
                'code' => $code,
                'grant_type' => 'authorization_code',
            ];
            $accessToken = curl($url,http_build_query($params));
            $accessToken = json_decode($accessToken)->access_token;

            $url = "https://api.linkedin.com/v1/people/~:(id,firstName,lastName,pictureUrls::(original),headline,publicProfileUrl,location,industry,positions,email-address )?format=json&oauth2_access_token=" . $accessToken;
            $user = file_get_contents($url, false);

     $User = json_decode(post_curl($url)); // Request user information on received token

$query = "INSERT INTO `linkedti_scheduler`.`users`
                   (`userid`,
                   `firstName`,
                   `lastName`,
                   `emailAddress`,
                   `position`,
                   `location`,
                   `profileURL`,
                   `headline`)

                   VALUES

                   ('$id',
           '$firstName',
           '$lastName',
           '$emailAddress',
           '$position',
           '$location',
           '$profileURL',
           '$headline')";
           mysqli_query($connection,$query);


            return (json_decode($user));



        }
    }
     ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-04 18:57:26

您需要使用$User对象引用字段,如下所示:

代码语言:javascript
复制
 $User = json_decode(post_curl($url)); // Request user information on received token

$query = "INSERT INTO `linkedti_scheduler`.`users`
               (`userid`,
               `firstName`,
               `lastName`,
               `emailAddress`,
               `position`,
               `location`,
               `profileURL`,
               `headline`)

               VALUES

               ('$id',
       '$User->firstName',
       '$User->lastName',
       '$User->emailAddress',
       '$User->position',
       '$User->location',
       '$User->profileURL',
       '$User->headline')";

另外,请注意这里没有使用MySQL转义--我建议使用准备好的查询,以防止单引号出现任何问题:

https://secure.php.net/manual/en/mysqli.quickstart.prepared-statements.php

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45733514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档