### Starting PHP Embedded Web Server Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md This command starts a PHP built-in web server on `localhost:8901`, serving files from the `examples` directory. This allows running the project's examples locally without a full web server setup. ```bash php -S localhost:8901 -t examples ``` -------------------------------- ### Installing PHP Dependencies with Composer Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md This command uses Composer to install all required PHP dependencies for the project. The optional `-d` flag allows specifying the repository root if not run from the current directory. ```bash composer install [-d /path/to/repository/root] ``` -------------------------------- ### Installing DotEnv Dependency with Composer Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md If the `Dotenv\Dotenv` class is not found, this command installs the `vlucas/phpdotenv` package via Composer. This package is crucial for loading environment variables from the `.env` file. ```bash composer require vlucas/phpdotenv ``` -------------------------------- ### Changing Directory to Project Root Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md After cloning, this command navigates into the newly created `linkedin-api-php-client` directory, which is the root of the project where further setup commands will be executed. ```bash cd linkedin-api-php-client ``` -------------------------------- ### Cloning Repository with Git Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md This command clones the `zoonman/linkedin-api-php-client` repository from GitHub to your local machine, making the project files available for setup and execution. ```bash git clone https://github.com/zoonman/linkedin-api-php-client ``` -------------------------------- ### Installing LinkedIn PHP Client via Composer Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This command installs the `zoonman/linkedin-api-php-client` package using Composer, the PHP dependency manager. It's the recommended way to add the library to your project, ensuring all necessary dependencies are resolved. ```bash composer require zoonman/linkedin-api-php-client ``` -------------------------------- ### Creating .env File with Bash Echo Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md These commands create or append the LinkedIn client ID and secret to the `.env` file using `echo`. This is a convenient way to quickly set up the environment variables from the command line. ```bash echo 'LINKEDIN_CLIENT_ID=111ClientId111' >> .env echo 'LINKEDIN_CLIENT_SECRET=222ClientSecret' >> .env ``` -------------------------------- ### Configuring LinkedIn API Credentials in .env (INI) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md This snippet shows the structure for the `.env` file, which holds sensitive LinkedIn API client ID and secret. These credentials are essential for authenticating with the LinkedIn API. ```ini LINKEDIN_CLIENT_ID=111ClientId111 LINKEDIN_CLIENT_SECRET=222ClientSecret ``` -------------------------------- ### Configuring OAuth 2.0 Authorized Redirect URL Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md This URL must be added to the OAuth 2.0 Authorized Redirect URLs in the LinkedIn Developers portal. It specifies where LinkedIn will redirect the user after successful authentication. ```text http://localhost:8901/ ``` -------------------------------- ### Performing GET Requests to LinkedIn API in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This helper method provides a simplified way to make HTTP GET requests to a specified LinkedIn API endpoint. It takes the endpoint URL and an optional array of query parameters, streamlining common data retrieval operations. ```php // get method $client->get('ENDPOINT', ['param' => 'value']); ``` -------------------------------- ### Storing LinkedIn Access Token to File in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This example demonstrates a basic way to store the obtained `LinkedIn\AccessToken` object to a `token.json` file. While convenient for demonstration, this method is not recommended for production due to security concerns; tokens should be stored securely. ```php file_put_contents('token.json', json_encode($accessToken)); ``` -------------------------------- ### Retrieving Personal Profile Information (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet demonstrates how to fetch basic profile information for the authenticated user from the LinkedIn API. It uses the `get` method with the 'me' endpoint and specifies desired fields like 'id', 'firstName', and 'lastName'. The result is then printed. ```PHP $profile = $client->get( 'me', ['fields' => 'id,firstName,lastName'] ); print_r($profile); ``` -------------------------------- ### Making Generic LinkedIn API Calls in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet demonstrates the generic `api()` method for making calls to any LinkedIn API endpoint. It accepts the endpoint URL, an array of parameters, and the HTTP method (e.g., 'GET', 'POST', 'DELETE') as arguments, providing flexibility for various API interactions. ```php $profile = $client->api( 'ENDPOINT', ['parameter name' => 'its value here'], 'HTTP method like GET for example' ); ``` -------------------------------- ### Sharing Content on Business Page (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This example shows how to publish an article share on a LinkedIn business page. It uses the `post` method to the 'ugcPosts' endpoint, specifying the organization's URN as the author and setting visibility to 'PUBLIC'. A placeholder `companyId` is provided for a sandboxed company page. ```PHP // set sandboxed company page to work with // you can check updates at // https://www.linkedin.com/company/devtestco $companyId = '2414183'; $share = $client->post( 'ugcPosts', [ 'author' => 'urn:li:organization:' . $companyId, 'lifecycleState' => 'PUBLISHED', 'specificContent' => [ 'com.linkedin.ugc.ShareContent' => [ 'shareCommentary' => [ 'text' => 'Checkout this amazing PHP SDK for LinkedIn!' ], 'shareMediaCategory' => 'ARTICLE', 'media' => [ [ 'status' => 'READY', 'description' => [ 'text' => 'OAuth 2 flow, composer Package.' ], 'originalUrl' => 'https://github.com/zoonman/linkedin-api-php-client', 'title' => [ 'text' => 'PHP Client for LinkedIn API' ] ] ] ] ], 'visibility' => [ 'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC' ] ] ); print_r($share); ``` -------------------------------- ### Sharing Content on Personal Profile (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This example illustrates how to publish an article share on the authenticated user's personal LinkedIn profile. It uses the `post` method to the 'ugcPosts' endpoint, including details like author, lifecycle state, share commentary, media (URL, description, title), and visibility settings. An image URL must be publicly accessible. ```PHP $share = $client->post( 'ugcPosts', [ 'author' => 'urn:li:person:' . $profile['id'], 'lifecycleState' => 'PUBLISHED', 'specificContent' => [ 'com.linkedin.ugc.ShareContent' => [ 'shareCommentary' => [ 'text' => 'Checkout this amazing PHP SDK for LinkedIn!' ], 'shareMediaCategory' => 'ARTICLE', 'media' => [ [ 'status' => 'READY', 'description' => [ 'text' => 'OAuth 2 flow, composer Package.' ], 'originalUrl' => 'https://github.com/zoonman/linkedin-api-php-client', 'title' => [ 'text' => 'PHP Client for LinkedIn API' ] ] ] ] ], 'visibility' => [ 'com.linkedin.ugc.MemberNetworkVisibility' => 'CONNECTIONS' ] ] ); print_r($share); ``` -------------------------------- ### Retrieving Company Page Profile (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet demonstrates how to fetch detailed information for a specific LinkedIn company page. It requires the company's ID (e.g., '123') and uses the `get` method on the 'organizations/{companyId}' endpoint. The retrieved company information is then printed. ```PHP $companyId = '123'; // use id of the company where you are an admin $companyInfo = $client->get('organizations/' . $companyId); print_r($companyInfo); ``` -------------------------------- ### Initializing LinkedIn Client in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet demonstrates how to include Composer's autoloader and instantiate the `LinkedIn\Client` class. It requires your LinkedIn application's client ID and client secret as parameters to set up the API client for further operations. ```php // ... please, add composer autoloader first include_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; // import client class use LinkedIn\Client; // instantiate the Linkedin client $client = new Client( 'YOUR_LINKEDIN_APP_CLIENT_ID', 'YOUR_LINKEDIN_APP_CLIENT_SECRET' ); ``` -------------------------------- ### Setting Stored Access Token for LinkedIn Client in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This code illustrates how to load a previously stored access token from a file, instantiate a `LinkedIn\AccessToken` object from its data, and then set it on the `LinkedIn\Client` instance. This allows the client to make authenticated API calls without re-initiating the OAuth flow. ```php use LinkedIn\AccessToken; use LinkedIn\Client; // instantiate the Linkedin client $client = new Client( 'LINKEDIN_APP_CLIENT_ID', 'LINKEDIN_APP_CLIENT_SECRET' ); // load token from the file $tokenString = file_get_contents('token.json'); $tokenData = json_decode($tokenString, true); // instantiate access token object from stored data $accessToken = new AccessToken($tokenData['token'], $tokenData['expiresAt']); // set token for client $client->setAccessToken($accessToken); ``` -------------------------------- ### Generating LinkedIn Login URL with Scopes in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This code demonstrates how to generate the LinkedIn login URL, which initiates the OAuth 2.0 flow. It requires an array of desired permissions (scopes) using the `LinkedIn\Scope` enum, allowing the application to request specific user data or actions. ```php use LinkedIn\Scope; // define scope $scopes = [ Scope::READ_LITE_PROFILE, Scope::READ_EMAIL_ADDRESS, Scope::SHARE_AS_USER, Scope::SHARE_AS_ORGANIZATION, ]; $loginUrl = $client->getLoginUrl($scopes); // get url on LinkedIn to start linking ``` -------------------------------- ### Attempting Image Upload (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet attempts to upload an image to LinkedIn using the `upload` method. It sets the API root to 'https://api.linkedin.com/' and provides a local file path. Note that this operation often requires specific LinkedIn partner permissions and may result in 'Not enough permissions' errors. ```PHP $filename = '/path/to/image.jpg'; $client->setApiRoot('https://api.linkedin.com/'); $mp = $client->upload($filename); ``` -------------------------------- ### Performing POST Requests to LinkedIn API in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This helper method facilitates making HTTP POST requests to a LinkedIn API endpoint. It accepts the endpoint URL and an array of data to be sent in the request body, commonly used for creating or updating resources. ```php //post $client->post('ENDPOINT', ['param' => 'value']); ``` -------------------------------- ### Obtaining OAuth Access Token in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet retrieves the OAuth 2.0 access token from LinkedIn using the authorization code obtained from the redirect URL. The `$_GET['code']` parameter is the authorization code provided by LinkedIn after the user grants permissions. ```php $accessToken = $client->getAccessToken($_GET['code']); ``` -------------------------------- ### Listing Administered Companies (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet shows how to retrieve a list of companies where the authenticated user has administrative privileges. It calls the 'organizations' endpoint with the 'is-company-admin' filter set to true. The response containing the company list is then printed. ```PHP $profile = $client->get( 'organizations', ['is-company-admin' => true] ); print_r($profile); ``` -------------------------------- ### Setting Custom API Request Headers (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet demonstrates how to configure custom HTTP headers for requests made to the LinkedIn API. It uses the `setApiHeaders` method to define headers like 'Content-Type', 'x-li-format', 'X-Restli-Protocol-Version' (for v2), and 'x-li-src' (to mimic a mobile SDK). ```PHP $client->setApiHeaders([ 'Content-Type' => 'application/json', 'x-li-format' => 'json', 'X-Restli-Protocol-Version' => '2.0.0', // use protocol v2 'x-li-src' => 'msdk' // set a src header to "msdk" to mimic a mobile SDK ]); ``` -------------------------------- ### Retrieving Default Redirect URL in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This line of code retrieves the default redirect URL configured for the LinkedIn client. This URL is crucial for the OAuth 2.0 flow, as LinkedIn will redirect the user back to this URL after authorization. ```php $redirectUrl = $client->getRedirectUrl(); ``` -------------------------------- ### Changing Default API Root (PHP) Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet shows how to modify the base URL for LinkedIn API requests. It uses the `setApiRoot` method to specify a different API endpoint, such as 'https://api.linkedin.com/v2/', which might be useful for accessing private or specific API versions. ```PHP $client->setApiRoot('https://api.linkedin.com/v2/'); ``` -------------------------------- ### Setting Custom Redirect URL in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This snippet shows how to explicitly set a custom redirect URL for the LinkedIn client. This is useful when the default URL is not suitable or when a specific callback endpoint is required for the OAuth 2.0 authorization process. ```php $client->setRedirectUrl('http://your.domain.tld/path/to/script/'); ``` -------------------------------- ### Performing DELETE Requests to LinkedIn API in PHP Source: https://github.com/zoonman/linkedin-api-php-client/blob/master/README.md This helper method allows for making HTTP DELETE requests to a specified LinkedIn API endpoint. It takes the endpoint URL as a parameter, typically used for removing resources. ```php // delete $client->delete('ENDPOINT'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.