smartstrategy/linkedin-php

dev-master 2020-08-26 22:14 UTC

This package is auto-updated.

Last update: 2024-08-29 05:43:24 UTC


README

LinkedIn API V2的PHP客户端。

要求

  • php >= 7.0

安装

composer require smart/linkedin-php:"dev-master"

使用LinkedIn API

要与LinkedIn API交互,必须初始化Client类。

$client = new Client('appId', 'appSecret', 'returnUrl');

认证

$client = new Client('appId', 'appSecret', 'returnUrl');
if (array_key_exists('code', $_GET)) {
    $client->initToken($_GET['code']);
    
    $me = new Me($client);
} else {
    $authUrl = $client->getAuthenticationUrl([
        'scope' => [Client::PERMISSION_LITE_PROFILE]
    ]);
    header('Location: '.$authUrl);
    exit;
}

分享

可以在LinkedIn活动上发布新帖子或分享帖子。

分享帖子

$client = new Client('appId', 'appSecret', 'returnUrl');
if (array_key_exists('code', $_GET)) {
    $client->initToken($_GET['code']);
    
    $shares = new Shares();
    $shares->setResharedShare('urn:li:share:1232132') // Post's urn:id
    
    $shares->setOwner('urn:li:person:c7RFYxyz78')
    
    $shareText = new ShareText();
    $shareText->setTitle('my title');
    
    $shares->setText($shareText);
    
    $shareEndpoint = new SMarT\LinkedIn\Endpoint\Share($client);
    $shareEndpoint->postShares($shares);
} else {
    $authUrl = $client->getAuthenticationUrl([
        'scope' => [Client::PERMISSION_LITE_PROFILE, Client::PERMISSION_W_MEMBER_SOCIAL]
    ]);
    header('Location: '.$authUrl);
    exit;
}

在执行此操作之前,必须在LinkedIn应用中认证在setOwner中声明的用户。