Search code examples
phpjsonrestlinkedin-api

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?


Solution

  • 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.