Andrew
Andrew

Reputation: 1

LinkedIn REST API - Company Share | Using JSON or XML, both fail

I've spent the day on this issue and read tons of answers on stackoverflow, but still can't figure it out.

I want to post on LinkedIn company page using the REST API. I already have a token retrieved through the OAuth2 API ($secret_token), I also have a company ID for which the user has admin rights ($company_ID), I have performed multiple "GET" via cURL without any problem.

Using the console (https://apigee.com/console/linkedin) I can do what I want. But once translated into PHP, I can't get it to work.

Here is my code:

// USING XML POST
$postfields = '
<xml>
    <share>
        <visibility>
            <code>anyone</code>
        </visibility>
        <comment>There are a lot of great career opportunities here!</comment>
    </share>
</xml>';

$headers = array('header' => 
    "Authorization: Bearer " .$secret_token. "\r\n".
    'Content-Length: ' . strlen($postfields). "\r\n".
    'Content-Type: text/xml'. "\r\n"
);

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://api.linkedin.com/v1/companies/".$company_id."/shares?format=json",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_HEADER => false,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => $headers, 
    CURLOPT_POSTFIELDS => $postfields
));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
print_r($result);

This displays:

Array ( [errorCode] => 0 [message] => Couldn't parse share document: error: Unexpected element: CDATA [requestId] => IWMFAAIY5E [status] => 400 [timestamp] => 1484563199667 )

Alright, now doing the same with JSON instead (since both are allowed by LinkedIn REST API):

// USING JSON POST
$postfields = '{"visibility":  {    "code": "anyone"  },  "comment": "There are a lot of great career opportunities here!"}';

$headers = array('header' => 
    "Authorization: Bearer " .$secret_token. "\r\n".
    'Content-Length: ' . strlen($postfields). "\r\n".
    'Content-Type: application/json'. "\r\n".
    "x-li-format: json". "\r\n"
);

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://api.linkedin.com/v1/companies/".$company_id."/shares?format=json",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_HEADER => false,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => $headers, 
    CURLOPT_POSTFIELDS => $postfields
));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
print_r($result);

It now displays:

Array ( [errorCode] => 0 [message] => Couldn't parse Json body: Unexpected character ('C' (code 67)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.StringReader@7e6528c1; line: 1, column: 2] [requestId] => HIRNMKAQTC [status] => 400 [timestamp] => 1484564171949 )

Any idea?

Upvotes: 0

Views: 660

Answers (1)

Matthew
Matthew

Reputation: 11

On your $headers variable, delete the ending ."\r\n"

$headers = array('header' => 
    "Authorization: Bearer " .$secret_token. "\r\n".
    'Content-Length: ' . strlen($postfields). "\r\n".
    'Content-Type: application/json'. "\r\n".
    "x-li-format: json"
);

Upvotes: 1

Related Questions