Auto Post on LinkedIn Using PHP OAuth


In previous tutorials, I have explained Auto Post on Twitter and Facebook. Now in this tutorial I will explain, how you can Auto Post on LinkedIn using PHP OAuth and LinkedIn API. As we already know how we can get user Access Token for LinkedIn, in this tutorial we will share the content on LinkedIn using this Access Token, PHP OAuth and LinkedIn API.

Get Access Token
You can get the Access Token by follow the instruction of get user Access Token for LinkedIn in serialized form.

Auto Post on LinkedIn
Now for the Auto Post script create a file autopost.php and write the following code:

require_once("OAuth.php");

$data = array(
    'consumer_key' => '{CLIENT_ID}',
    'consumer_secret' => '{CLIENT_SECRET}',
    'callback_url' => ''
);

$method = new OAuthSignatureMethod_HMAC_SHA1();
$consumer = new OAuthConsumer($data['consumer_key'], $data['consumer_secret']);

$access_token = unserialize($access_token); // as we got from last tutorial <a href="http://www.tricksofit.com/2015/09/get-user-access-token-for-linkedin" target="_blank"><strong>get user Access Token for LinkedIn</strong></a>

$title = "Auto Post on LinkedIn Using PHP OAuth";
$targetUrl = "http://www.tricksofit.com/2015/09/auto-post-on-linkedin-using-php-oauth";
$imgUrl = "http://www.tricksofit.com/wp-content/uploads/2015/09/Auto-Post-on-LinkedIn-Using-PHP-OAuth.png";
$description= "In this tutorial I will show you how to Auto Post on LinkedIn using linkedIn Access Token and LinkedIn API with OAuth PHP Library.";

$shareUrl = "http://api.linkedin.com/v1/people/~/shares";
$xml = "<share>";
$xml .= "<content>
<title>$title</title>
<description>$description</description>";
if(!empty($targetUrl)){
    $xml .= "<submitted-url>$targetUrl</submitted-url>";
}

if(!empty($imgUrl)){
    $xml .= "<submitted-image-url>$imgUrl</submitted-image-url>";
}
$xml .= "</content>
  <visibility>
    <code>anyone</code>
  </visibility>
</share>";

$request = OAuthRequest::from_consumer_and_token($consumer, $access_token, "POST", $shareUrl);
$request->sign_request($method, $consumer, $access_token);
$auth_header = $request->to_header("https://api.linkedin.com");

$response = httpRequest($shareUrl, $auth_header, "POST", $xml);

function httpRequest($url, $auth_header, $method, $body = NULL) {
    if (!$method) {
        $method = "GET";
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header)); // Set the headers.

    if ($body) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header, "Content-Type: text/xml;charset=utf-8"));  
    }

    $data = curl_exec($curl);
    curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    return $data;
}

You can see this in your activities. Here is the screen of the content shared on the user’s LinkedIn account.

auto post share content
Auto post share content

Hope this tutorial will help you to Auto Post on LinkedIn.

  • Nilamadhab Khanda

    hi im geting this error

    Fatal error: Uncaught exception ‘FacebookExceptionsFacebookSDKException’ with message ‘Required “app_id” key not supplied in config and could not find fallback environment variable “FACEBOOK_APP_ID”‘ in /home/sourcecc/public_html/facebook/Facebook/src/Facebook/Facebook.php:133 Stack trace: #0 /home/sourcecc/public_html/facebook/getAccessToken.php(13): FacebookFacebook->__construct(Array) #1 {main} thrown in/home/sourcecc/public_html/facebook/Facebook/src/Facebook/Facebook.php on line 133

  • Baral Manish

    hi,

    where can I find OAuth.php?

  • Shital Pimpale

    i got this error : Invalid arguments: {S_400_BAD_REQUEST=Bad request}

  • Marco Floriano

    Nice trick, thanks!

  • ericross2

    When I send a post using this tutorial, I can see through my Developers’ “Usage & Limits” that a post was sent (the post share number increases by 1), but I don’t see the post appear either on my personal page (no updates) nor on my company page (same). I believe I am acquiring the token correctly (using your prior tutorial), and it asked for my permission to post as me on LinkedIn (the dialog box appeared — it should only ask me once at the very first time running this, right?). I am logged in on LinkedIn on this PC but the PHP is run from a server and called by this PC’s browser. Any thoughts as to how I can fix this?

  • Developer D

    Hi, I’m getting 401 [unauthorized]. The token used in the OAuth request is not valid. consumerKey.
    I followed this documentation and script. Also I tested the access token – it’s a valid token. Please advice me. Thank you!