Auto Post On LinkedIn Using PHP script in 2024

Auto Post On LinkedIn Using PHP script

Today, We want to share with you Auto Post On LinkedIn Using PHP script.
In this post we will show you Automatically Post Content To LinkedIn Using PHP, hear for Adding a post : Auto Post On LinkedIn Using PHP we will give you demo and example for implement.
In this post, we will learn about Automatically post to a LinkedIn page using the LinkedIn PHP API with an example.

LinkedIn Get Access Token using PHP

You can Best Way to get the LinkedIn Access Token by follow the Step By Step of This Link Get Access Tokens – LinkedIn Login – LinkedIn for Developers in serialized form.

get user access token for linkedin 4
get user access token for linkedin 4

Auto Post on LinkedIn using PHP Script

First of all for the Simple Example of the LinkedIn Auto Post script make a PHP file autopost.php and then Copy Past the following below Source code:

//include here OAuth.php File
require_once("OAuth.php");
 
$config = array(
    'consumer_key' => '{YOUR_LIVE_CLIENT_ID}',
    'consumer_secret' => '{YOUR_KEY_OF_THE_CLIENT_SECRET}',
    'callback_url' => ''
);
 
$req_mthd = new OAuthSignatureMethod_HMAC_SHA1();
$live_consume = new OAuthConsumer($config['consumer_key'], $config['consumer_secret']);
 
$access_token = unserialize($access_token); 

http://www.pakaInfo.com/2020/09/get-user-access-token-for-linkedin" target="_blank">get user Access Token for LinkedIn
 
$title = "Auto Post On Blogspot Using PHP script";
$YourtargetURI = "http://www.pakaInfo.com/2020/09/auto-post-on-linkedin-using-php-oauth";
$imgUrl = "http://www.pakaInfo.com/wp-content/uploads/2020/09/Auto-Post-on-LinkedIn-Using-PHP-OAuth.png";
$description= "PakaInfo was founded in January 2018. We deliver Jobs, Earn Money, Education, Health, Technology, Life Style, Science, Top10 and Nature.";
 
$live_pakainfo_url = "http://api.linkedin.com/v1/people/~/shares";
$xml = "";
$xml .= "
$title
$description";
if(!empty($YourtargetURI)){
    $xml .= "$YourtargetURI";
}
 
if(!empty($imgUrl)){
    $xml .= "$imgUrl";
}
$xml .= "
  
    anyone
  
";
 
$live_req = OAuthRequest::from_consumer_and_token($live_consume, $access_token, "POST", $live_pakainfo_url);
$live_req->sign_request($req_mthd, $live_consume, $access_token);
$linkdin_auth_head = $live_req->to_header("https://api.linkedin.com");
 
$response = httpRequest($live_pakainfo_url, $linkdin_auth_head, "POST", $xml);
 
function httpRequest($url, $linkdin_auth_head, $req_mthd, $body = NULL) {
    if (!$req_mthd) {
        $req_mthd = "GET";
    }
 
    $live_ch = curl_init();
    curl_setopt($live_ch, CURLOPT_URL, $url);
    curl_setopt($live_ch, CURLOPT_HEADER, 0);
    curl_setopt($live_ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($live_ch, CURLOPT_HTTPHEADER, array($linkdin_auth_head)); // Set the all Your LinkedIn headers.
 
    if ($body) {
        curl_setopt($live_ch, CURLOPT_POST, 1);
        curl_setopt($live_ch, CURLOPT_POSTFIELDS, $body);
        curl_setopt($live_ch, CURLOPT_CUSTOMREQUEST, $req_mthd);
        curl_setopt($live_ch, CURLOPT_HTTPHEADER, array($linkdin_auth_head, "Content-Type: text/xml;charset=utf-8"));  
    }
 
    $resData = curl_exec($live_ch);
    curl_getinfo($live_ch, CURLINFO_HTTP_CODE);
    curl_close($live_ch);
    return $resData;
}

You can Display this in your All the LinkedIn activities. Here is the Latest My LinkedIn screen of the Share Data or Logo content shared on the Live Users using LinkedIn account.

Auto Post On LinkedIn Using PHP script
Auto Post On LinkedIn Using PHP script

Auto Post On LinkedIn Using PHP

Auto posting on LinkedIn with PHP is not officially supported by LinkedIn, and it goes against their terms of service. However, there are third-party tools and libraries available that can automate the process. Here’s an example of how to auto post on LinkedIn using PHP:

First, you’ll need to create a LinkedIn developer account and register an application to obtain an access token. You can follow the instructions in the LinkedIn documentation to do this: https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context.

Once you have an access token, you can use the LinkedIn API to post a message. Here’s an example using the PHP library “linkedin-api-php-client” (https://github.com/zoonman/linkedin-api-php-client):

use LinkedIn\Client;
use LinkedIn\Scope;
use LinkedIn\AccessToken;

$client = new Client(
    'YOUR_APP_ID',
    'YOUR_APP_SECRET'
);

$accessToken = new AccessToken([
    'access_token' => 'YOUR_ACCESS_TOKEN',
    'expires_in' => 'TOKEN_EXPIRATION_TIME_IN_SECONDS'
]);

$client->setAccessToken($accessToken);

$scope = [Scope::WRITE_SHARE];
$response = $client->request('POST', '/v2/shares', [
    'json' => [
        'owner' => 'urn:li:person:YOUR_USER_ID',
        'text' => [
            'text' => 'Your message here'
        ]
    ],
    'query' => [
        'oauth2_access_token' => $accessToken->getToken()
    ],
    'headers' => [
        'X-Restli-Protocol-Version' => '2.0.0'
    ]
]);

$responseBody = json_decode($response->getBody(), true);

In this example, the LinkedIn API is used to post a message on behalf of a user. You’ll need to replace YOUR_APP_ID, YOUR_APP_SECRET, YOUR_ACCESS_TOKEN, and YOUR_USER_ID with the appropriate values. The text parameter contains the message you want to post.

Note that auto posting on LinkedIn is not officially supported, and LinkedIn may take action against accounts that violate their terms of service. Use these scripts at your own risk and always follow LinkedIn’s guidelines.

We hope you get an idea about Auto post into LinkedIn using php Source code
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.

Leave a Comment