How to Publish Status on LinkedIn in PHP
We have received many requests from my readers to write tutorial on LinkedIn status update in PHP so this tutorial going to solve your problems and its very easy to publish status on your LinkedIn wall. No need to go to LinkedIn and update your status from your website create a file on your web site which do this all stuff without going to LinkedIn.
We are not going into deep of LinkedIn API configuration you can get API integration and login details from our previous post How to Login with LinkedIn OAuth in PHP follow API instruction and configure your API.
After LinkedIn API creation you will get LinkedIn API Key and Secret Key which you need to add in config file as below.
Directory Structure:
PHP Code
Edit config.php
<?php $config['base_url'] = 'http://www.yourwebsite.com/post-status-on-linkedin'; //linkedin configuration $config['linkedin_access'] = 'your_linkedin_access'; // Add your application access code $config['linkedin_secret'] = 'your_linkedin_secret'; // Add your application secret code $config['linkedin_library_path']= 'linkedinoAuth.php'; ?>
In this file you have to give your API credentials.
Index.php
<?php session_start(); require_once('oAuth/linkedinoAuth.php'); require_once('oAuth/class.linkedClass.php'); require_once('oAuth/config.php'); if ($config['linkedin_access'] === '' || CONSUMER_SECRET === '') { echo 'You need a API Key and Secret Key to test the sample code. Get one from <a href="https://www.linkedin.com/secure/developer">https://www.linkedin.com/secure/developer</a>'; exit; } if(isset($_POST['status'])) { $linkedClass = new linkedClass(); $status = substr($_POST['status'], 0, 144); $linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret']); $message = 'Status updated.<br>'; $linkedClass->linkedInStatus($status, $_SESSION['requestToken'], $_SESSION['oauth_verifier'], $_SESSION['oauth_access_token']); } elseif(isset($_GET['login'])) { $linkedClass = new linkedClass(); # First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback $linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret']); //$linkedin->debug = true; if (isset($_REQUEST['oauth_verifier'])) { $_SESSION['oauth_verifier'] = $_REQUEST['oauth_verifier']; $linkedin->request_token = unserialize($_SESSION['requestToken']); $linkedin->oauth_verifier = $_SESSION['oauth_verifier']; $linkedin->getAccessToken($_REQUEST['oauth_verifier']); $_SESSION['oauth_access_token'] = serialize($linkedin->access_token); } else { $linkedin->request_token = unserialize($_SESSION['requestToken']); $linkedin->oauth_verifier = $_SESSION['oauth_verifier']; $linkedin->access_token = unserialize($_SESSION['oauth_access_token']); } //////////////////////////////////////////////////////////// ////////////////////// form goes here.////////////////////// //////////////////////////////////////////////////////////// } else { $content = '<a href="linkedin.php"><img src="./images/linkedin_connect_button.png" alt="Sign in with LinkedIn"/></a>'; } ?>
This file can do the magic first of all show you a login with LinkedIn and once you login save all your secret keys in session and server where we need, show you a text box to update your status in that box insert your comment and post it will go to your LinkedIn wall.
Created class object $linkedClass = new linkedClass(); and create LinkedIn library class object to assign keys.
$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret']);
Call post status method
$linkedClass->linkedInStatus($status, $_SESSION['requestToken'], $_SESSION['oauth_verifier'], $_SESSION['oauth_access_token']);
LinkedIn allow you only 144 latter for a single post so we use this function to limit status to 144 latter substr($_POST[‘status’], 0, 144); .
Video:
Your status on LinkedIn look like this.
This is the all explanation I hope you guys understand from this tutorial and it works for you. We have add this tutorial with demo and you can also download its source code
I love to answer your questions and so please feel free to comment below and do suggest us for more improvement in tutorials and demos.
Tutorial Categories:
I have the same error. DId you found any solution ?
hey, it saves the token, i want my app to ask for allow every time it post. Can you please tell the changes i have to do?
how do you get the oath token for the application? everytime I press the connect with LinkedIn it says authorization error.
you have to hug your mom, then lick your dog and jump from a 10story building.
We have successfully achieved oauth authentication using linkedin but after publishing the status it shows “Status Updated” and it will take effect after 1 minute but after many hours it is still not there. Where we are wrong?
API is not working even your demo link is not working too.
can you please send me all file and code that post image on linkedin using php. It must need for me. please send me…