加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何使用PHP获取带有LinkedIn注册API的电子邮件地址

发布时间:2020-12-13 22:22:52 所属栏目:PHP教程 来源:网络整理
导读:我从一些关于链接注册的教程中获得了这段代码,但是教程只提供了基本信息.我还需要收到用户的电子邮件……我该怎么做? 这是代码: auth.php 这是我在likesin上访问表单的链接. ?php session_start(); $config['base_url'] = ''; $config['callback_url'] = '
我从一些关于链接注册的教程中获得了这段代码,但是教程只提供了基本信息.我还需要收到用户的电子邮件……我该怎么做?

这是代码:

auth.php

这是我在likesin上访问表单的链接.

<?php
    session_start();
    $config['base_url']             =   '';
    $config['callback_url']         =   '';
    $config['linkedin_access']      =   '';
    $config['linkedin_secret']      =   '';

    include_once "linkedin.php";

    # First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
    $linkedin = new LinkedIn($config['linkedin_access'],$config['linkedin_secret'],$config['callback_url'] );
    //$linkedin->debug = true;

    # Now we retrieve a request token. It will be set as $linkedin->request_token
    $linkedin->getRequestToken();
    $_SESSION['requestToken'] = serialize($linkedin->request_token);

    # With a request token in hand,we can generate an authorization URL,which we'll direct the user to
    //echo "Authorization URL: " . $linkedin->generateAuthorizeUrl() . "nn";
    header("Location: " . $linkedin->generateAuthorizeUrl()); ?>

demo.php

这是我在注册后获得的脚本.

<?php
    session_start();

    $config['base_url']             =   'http://xxx/linkedin/auth.php';
    $config['callback_url']         =   'http://xxx/linkedin/demo.php';
    $config['linkedin_access']      =   '';
    $config['linkedin_secret']      =   '';

    include_once "linkedin.php";

    # First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
    $linkedin = new LinkedIn($config['linkedin_access'],$config['callback_url'] );
    //$linkedin->debug = true; if (isset($_REQUEST['oauth_verifier'])){
    $_SESSION['oauth_verifier']     = $_REQUEST['oauth_verifier'];

    $linkedin->request_token    =   unserialize($_SESSION['requestToken']);
    $linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
    $linkedin->getAccessToken($_REQUEST['oauth_verifier']);

    $_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
    header("Location: " . $config['callback_url']);
    exit;}   else{
    $linkedin->request_token    =   unserialize($_SESSION['requestToken']);
    $linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
    $linkedin->access_token     =   unserialize($_SESSION['oauth_access_token']);}

    # You now have a $linkedin->access_token and can make calls on behalf of the current member
    $xml_response = $linkedin->getProfile("~:(id,first-name,last-name,headline,picture-url)");


    $id = $linkedin->getProfile('~:(id)');
    $fname = $linkedin->getProfile('~:(first-name)');
    $lname = $linkedin->getProfile('~:(last-name)');
    $headline = $linkedin->getProfile('~:(headline)');
    $picture = $linkedin->getProfile('~:(picture-url)');

    $id = trim(strip_tags($id));
    $fname = trim(strip_tags($fname));
    $lname = trim(strip_tags($lname));
    $headline = trim(strip_tags($headline));
    $picture = trim(strip_tags($picture)); ?>

linkedin.php

这是LinkedIn库:

<?php require_once("OAuth.php"); class LinkedIn {
    public $base_url = "http://api.linkedin.com";
    public $secure_base_url = "https://api.linkedin.com";
    public $oauth_callback = "oob";
    public $consumer;
    public $request_token;
    public $access_token;
    public $oauth_verifier;
    public $signature_method;
    public $request_token_path;
    public $access_token_path;
    public $authorize_path;

    function __construct($consumer_key,$consumer_secret,$oauth_callback = NULL)
    {

        if($oauth_callback) {
            $this->oauth_callback = $oauth_callback;
        }

        $this->consumer = new OAuthConsumer($consumer_key,$this->oauth_callback);
        $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1();
        $this->request_token_path = $this->secure_base_url . "/uas/oauth/requestToken";
        $this->access_token_path = $this->secure_base_url . "/uas/oauth/accessToken";
        $this->authorize_path = $this->secure_base_url . "/uas/oauth/authorize";
    }

    function getRequestToken()
    {
        $consumer = $this->consumer;
        $request = OAuthRequest::from_consumer_and_token($consumer,NULL,"GET",$this->request_token_path);
        $request->set_parameter("oauth_callback",$this->oauth_callback);
        $request->sign_request($this->signature_method,$consumer,NULL);
        $headers = Array();
        $url = $request->to_url();
        $response = $this->httpRequest($url,$headers,"GET");
        parse_str($response,$response_params);
        $this->request_token = new OAuthConsumer($response_params['oauth_token'],$response_params['oauth_token_secret'],1);
    }

    function generateAuthorizeUrl()
    {
        $consumer = $this->consumer;
        $request_token = $this->request_token;
        return $this->authorize_path . "?oauth_token=" . $request_token->key;
    }

    function getAccessToken($oauth_verifier)
    {
        $request = OAuthRequest::from_consumer_and_token($this->consumer,$this->request_token,$this->access_token_path);
        $request->set_parameter("oauth_verifier",$oauth_verifier);
        $request->sign_request($this->signature_method,$this->consumer,$this->request_token);
        $headers = Array();
        $url = $request->to_url();
        $response = $this->httpRequest($url,$response_params);
        $this->access_token = new OAuthConsumer($response_params['oauth_token'],1);
    }

    function getProfile($resource = "~")
    {
        $profile_url = $this->base_url . "/v1/people/" . $resource;
        $request = OAuthRequest::from_consumer_and_token($this->consumer,$this->access_token,$profile_url);
        $request->sign_request($this->signature_method,$this->access_token);
        $auth_header = $request->to_header("https://api.linkedin.com"); # this is the realm
        # This PHP library doesn't generate the header correctly when a realm is not specified.
        # Make sure there is a space and not a comma after OAuth
        // $auth_header = preg_replace("/Authorization: OAuth,/","Authorization: OAuth ",$auth_header);
        // # Make sure there is a space between OAuth attribute
        // $auth_header = preg_replace('/",/','",',$auth_header);

        // $response will now hold the XML document
        $response = $this->httpRequest($profile_url,$auth_header,"GET");
        return $response;
    }

    function setStatus($status)
    {
        $profile_url = $this->base_url . "/v1/people/~";
        $status_url = $this->base_url . "/v1/people/~/current-status";
        echo "Setting status...n";
        $xml = "<current-status>" . htmlspecialchars($status,ENT_NOQUOTES,"UTF-8") . "</current-status>";
        echo $xml . "n";
        $request = OAuthRequest::from_consumer_and_token($this->consumer,"PUT",$status_url);
        $request->sign_request($this->signature_method,$this->access_token);
        $auth_header = $request->to_header("https://api.linkedin.com");

        $response = $this->httpRequest($profile_url,"GET");
        return $response;
    }

    # Parameters should be a query string starting with "?"
    # Example search("?count=10&start=10&company=LinkedIn");
    function search($parameters)
    {
        $search_url = $this->base_url . "/v1/people-search:(people:(id,picture-url,site-standard-profile-request,headline),num-results)" . $parameters;
        //$search_url = $this->base_url . "/v1/people-search?keywords=facebook";

        echo "Performing search for: " . $parameters . "<br />";
        echo "Search URL: $search_url <br />";
        $request = OAuthRequest::from_consumer_and_token($this->consumer,$search_url);
        $request->sign_request($this->signature_method,$this->access_token);
        $auth_header = $request->to_header("https://api.linkedin.com");
        $response = $this->httpRequest($search_url,"GET");
        return $response;
    }

    function httpRequest($url,$method,$body = NULL)
    {
        if (!$method) {
            $method = "GET";
        };

        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_HEADER,0);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_HTTPHEADER,array($auth_header)); // Set the headers.

        if ($body) {
            curl_setopt($curl,CURLOPT_POST,1);
            curl_setopt($curl,CURLOPT_POSTFIELDS,$body);
            curl_setopt($curl,CURLOPT_CUSTOMREQUEST,$method);
            curl_setopt($curl,array($auth_header,"Content-Type: text/xml;charset=utf-8"));
        }

        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }}

解决方法

电子邮件地址仅供经过身份验证的用户使用,而不适用于他/她的连接.

>确保在授权应用程序时请求用户提供r_emailaddress成员权限. Authentication详细介绍了身份验证和授予成员权限.
>进行以下GET调用以获取经过身份验证的用户的电子邮件地址:

GET http://api.linkedin.com/v1/people/~:(email-address)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读