jackbutler / php-linkedin
一个用于与LinkedIn API交互的PHP库
This package is not auto-updated.
Last update: 2024-09-28 19:09:42 UTC
README
一个提供与LinkedIn API简单交互的PHP库
安装
通过composer安装此包
运行 composer require jackbutler/php-linkedin
手动安装
将此包下载到您的项目中,并使用以下方式包含
<?php require 'path/to/LinkedIn/autoload.php';
使用方法
在进行API调用之前,您的应用程序应首先通过用户认证以与LinkedIn交互,即通过让用户授予您授权码,然后您可以将其交换为更长时间有效的访问令牌(目前为60天)。建议的过程如下
-
通过LinkedIn登录
该库包含一个生成登录URL的方法,以向用户提供“通过LinkedIn登录”按钮/链接。要生成此URL,您首先需要创建LinkedIn类的新实例
<?php $args = array ( 'clientId' => 'client_id', // Your LinkedIn API Client Id (required) 'clientSecret' => 'client_secret', // Your LinkedIn API Client Secret (required) 'callback' => 'URL/to/callback' // The absolute URL to your application's callback handler (required for auth requests) ); $linkedin = new PHPLI\Linkedin($args);
然后,您可以使用此实例生成登录URL,用于所需的范围。例如
<?php $scope = array ( "r_basicprofile", "r_emailaddress" ); $signinUrl = $linkedin->getLoginURL($scope);
然后,您可以将此URL提供给用户以使用LinkedIn进行登录
-
交换授权令牌为访问令牌
第一步将引导用户通过LinkedIn的登录和授权过程,然后返回您提供的回调URL,并带有授权令牌(假设一切顺利)。在您的回调脚本中,您需要将短暂的授权令牌交换为更长时间有效的访问令牌(目前为60天)。
该库提供了一个登录处理程序,可以在回调脚本中使用,如下所示
<?php // Instantiate a LinkedIn object as step 1, then: $token = $linkedin->handleLogin();
此方法将尝试从URL查询字符串(通过
$_GET)中检索授权码和CSRF令牌,然后内部调用getAccessToken()方法进行交换。除了返回访问令牌外,令牌还被保存为LinkedIn对象的属性(在getAccessToken()调用期间),为后续请求准备对象。访问令牌将在该实例的生命周期内存储,因此如果您的应用程序需要将其用于后续请求,则需要存储访问令牌。 -
向API发出请求
要向API发出请求,您将再次需要PHPLI\LinkedIn的实例,但这次在实例化时也设置了访问令牌
<?php $args = array ( 'clientId' => 'client_id', // Your LinkedIn API Client Id (required) 'clientSecret' => 'client_secret', // Your LinkedIn API Client Secret (required) 'accessToken' => 'the_access_token' // The access token authorised to make requests on the user's behalf ); $linkedin = new PHPLI\Linkedin($args);
一旦您有了LinkedIn实例,您可以使用
request()方法进行请求。以下给出的是request()方法的参数<?php request( String resource, String method = "GET", Array data = [] );
以下是一个从LinkedIn获取登录用户的一些属性的示例
<?php $response = $linkedin->request("/people/~:(first-name,last-name,id,email-address)");
开发
我开发了这个库作为我正在工作的另一个项目的一部分,但我自己很难找到一个合适的库来与LinkedIn交互,所以我决定将其提取出来,并为他人打包使用。我计划维护并改进它,我想要添加一些辅助类,例如LinkedInUser类(如果有人使用过Facebook PHP SDK,我希望将其构建成类似的东西)。
请随意fork此项目,提交改进的pull请求等。
许可
本项目采用MIT许可证。