Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedIn REST API - Internal server error

I'm using the LinkedIn REST API to post updates to a users timeline. Since a few days I get an Internal server error response from LinkedIn but the code worked before.

PHP:

$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";

$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json'
);

curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

How to fix that error?

like image 228
Tom Avatar asked Apr 16 '15 18:04

Tom


Video Answer


2 Answers

Your code is invalid PHP (perhaps because of some edits you made before posting?); modifying it to:

$postTitle = "hello";
$postDesc = "world ";
$postURL = "http://example.com";
$postImage = "http://images.example.com/img/hello.jpg";
$postComment = "foobar";

$oauthToken = "<token>";

$postData = array(
    'visibility' => array('code' => 'connections-only'),
    'content' => array(
        'title' => $postTitle,
        'description' => $postDesc,
        'submitted-url' => $postURL,
        'submitted-image-url' => $postImage
    ),
    'comment' => $postComment
);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json');

curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
    CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

works if only $oauthToken is set to a valid token. Assuming your real code is correct the only possiblity left is that your OAuth token has expired and you need to obtain a new one first. By adding CURLOPT_VERBOSE => TRUE to the cURL options you would find out more about the error that LinkedIn returns.

like image 185
Hans Z. Avatar answered Sep 21 '22 00:09

Hans Z.


You may considering using the LinkedIn PHP SDK (provided by the community) instead: https://github.com/Happyr/LinkedIn-API-client

like image 36
William Cheng Avatar answered Sep 22 '22 00:09

William Cheng