avansaber/php-linkedin-api
Composer 安装命令:
composer require avansaber/php-linkedin-api
包简介
Modern, fluent, framework-agnostic PHP client for the LinkedIn API (Marketing Developer Platform)
README 文档
README
Modern, fluent, framework-agnostic PHP client for the LinkedIn API (Marketing Developer Platform).
Installation
composer require avansaber/php-linkedin-api
Requires PHP >= 8.1. This is a library; do not commit composer.lock in apps consuming this library.
Getting Started
- Create a LinkedIn Developer App and request access to the Marketing Developer Platform if needed.
- Obtain Client ID and Client Secret.
- Configure OAuth Redirect URI.
Authentication (Authorization Code Flow, PKCE optional)
use Avansaber\LinkedInApi\Auth\Auth; use Avansaber\LinkedInApi\Auth\Pkce; use Avansaber\LinkedInApi\Auth\Scope; $auth = new Auth(); $state = bin2hex(random_bytes(16)); $authUrl = $auth->getAuthUrl( clientId: 'your-client-id', redirectUri: 'https://your-app/callback', scopes: [Scope::R_LITEPROFILE->value, Scope::W_MEMBER_SOCIAL->value], pkce: null, // or new Pkce($codeVerifier, $codeChallenge) state: $state ); // Redirect user to $authUrl // On callback, exchange the code for a token (perform the HTTP request using your HTTP client) $params = $auth->getAccessToken('client-id', 'client-secret', $_GET['code'], 'https://your-app/callback', null); // $params['endpoint'] and $params['params'] contain the token endpoint and form params
Client Setup
use Avansaber\LinkedInApi\Http\ClientConfig; use Avansaber\LinkedInApi\Http\LinkedInApiClient; use Nyholm\Psr7\Factory\Psr17Factory; use Http\Client\Curl\Client as CurlClient; // any PSR-18 client $psr17 = new Psr17Factory(); $http = new CurlClient(); $config = new ClientConfig(linkedInVersion: '202401'); $client = new LinkedInApiClient($http, $psr17, $psr17, $config, 'ACCESS_TOKEN');
Usage Examples
Fetch profile (Me)
use Avansaber\LinkedInApi\Resources\Me; $me = new Me($client); $profile = $me->get(); // tries /rest/me then falls back to /v2/me
Fetch organization
use Avansaber\LinkedInApi\Resources\Organizations; $orgs = new Organizations($client); $org = $orgs->get(123456);
Create UGC post
use Avansaber\LinkedInApi\Resources\UgcPosts; use Avansaber\LinkedInApi\Data\Requests\PostCreateRequest; $ugc = new UgcPosts($client); $resp = $ugc->create(new PostCreateRequest('urn:li:organization:123', 'Hello World'));
Get post (REST)
use Avansaber\LinkedInApi\Resources\Posts; $posts = new Posts($client); $post = $posts->get('urn:li:ugcPost:abc');
SocialActions (comments/likes)
use Avansaber\LinkedInApi\Resources\SocialActions; $sa = new SocialActions($client); $comments = $sa->comments('urn:li:ugcPost:abc'); $likes = $sa->likes('urn:li:ugcPost:abc');
Pagination iterator
use Avansaber\LinkedInApi\Http\PaginatorIterator; $iter = PaginatorIterator::iterate(function(int $start, int $count) use ($client) { return $client->get('organizations?q=vanityName&vanityName=linkedin&start='.$start.'&count='.$count, [], false); }, 10); foreach ($iter as $row) { /* ... */ }
Media uploads (initialize + PUT)
use Avansaber\LinkedInApi\Media\MediaUploadHelper; $helper = new MediaUploadHelper($client, $psr17); $init = $helper->initializeImageUpload('urn:li:organization:123'); $uploadUrl = $init['value']['uploadUrl']; $helper->uploadBinary($uploadUrl, file_get_contents('/path/image.jpg'));
Error Handling
Typed exceptions are thrown on non-2xx responses:
- AuthenticationException (401)
- PermissionException (403)
- NotFoundException (404)
- ValidationException (400)
- RateLimitException (429) with Retry-After
- ServerException (5xx)
The client captures X-LI-UUID from responses for troubleshooting.
Scopes & Access
- Member/profile:
r_liteprofile,r_emailaddress,w_member_social - Organization:
r_organization_social,w_organization_social,rw_organization_admin - Marketing/Ads (availability varies):
r_ads,rw_ads,r_campaigns,rw_campaigns
Many Marketing Developer Platform endpoints require partner/whitelisting.
CI & Quality
- PHPUnit test suite with retry/backoff tests
- PHPStan static analysis
- GitHub Actions matrix for PHP 8.1–8.3
Roadmap
- Chunked media upload (videos/documents)
- More resources and request DTOs (campaign creation/update)
- Integration tests examples
Modern LinkedIn API Support (2025 Update)
This package now supports LinkedIn's modern OpenID Connect API while maintaining backward compatibility.
✅ Recommended Modern Scopes
use Avansaber\LinkedInApi\Auth\Scope; // Modern OpenID Connect scopes (recommended) $scopes = [ Scope::OPENID->value, // Use your name and photo Scope::PROFILE->value, // Use your name and photo Scope::EMAIL->value, // Use the primary email address ]; // Or use the helper method $scopes = Scope::getModernProfileScopes();
📱 Modern Profile Fetching
use Avansaber\LinkedInApi\Resources\Me; $me = new Me($client); // Modern approach - uses OpenID Connect userinfo endpoint $profile = $me->getUserInfo(); // Recommended // Response format (OpenID Connect standard): // { // "sub": "linkedin-user-id", // "name": "John Doe", // "given_name": "John", // "family_name": "Doe", // "email": "john@example.com", // "picture": "https://..." // }
⚠️ Migration from Legacy API
| Legacy Scope | Modern Equivalent |
|---|---|
r_liteprofile |
openid + profile |
r_emailaddress |
email |
The package automatically handles fallbacks from modern to legacy endpoints for maximum compatibility.
avansaber/php-linkedin-api 适用场景与选型建议
avansaber/php-linkedin-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「linkedin」 「marketing」 「ads」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 avansaber/php-linkedin-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 avansaber/php-linkedin-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 avansaber/php-linkedin-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Linkedin API integration for Laravel and Lumen 5
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
LinkedIn integration for Social
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 42
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-08