2

Many code samples and tools for the LinkedIn REST API suppose that you have used Oauth1 to identify and that you consequently have obtained a Member Token and Member Secret.

Now, the sample code provided here by LinkedIn : http://developer.linkedin.com/documents/code-samples enables to get an Oauth2 access_token.

Once you have the access_token, you can make query and get member's details using the fetch function provided in the code sample

function fetch($method, $resource, $body = '') {


 $params = array('oauth2_access_token' => $_SESSION['access_token'],
                    'format' => 'json',
              );

    // Need to use HTTPS
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
    // Tell streams to make a (GET, POST, PUT, or DELETE) request
    $context = stream_context_create(
                    array('http' =>
                        array('method' => $method,
                        )
                    )
                );


    // Hocus Pocus
    $response = file_get_contents($url, false, $context);

    // Native PHP object, please
    return json_decode($response);
}

OK, great, this works perfectly. But the prodived fetch function assumes that body is NULL. But to post a Network update using /v1/people/~/person-activities, you need to pass some XML in the body. I have tried many ways, found dozens of example using Oauth 1.0 member token and member secret. But I have not found any solution that works when you only have an Oauth2 access_token. So the question is : What changes are required to this fetch function so that it can pass a XML body in the query ?

1 Answer 1

3

Finally, I found the answer.

function GetXMLTemplate(){
   string = '<?xml version="1.0" encoding="UTF-8"?><activity locale="en_US">
   <content-type>linkedin-html</content-type>
   <body>xml_content</body>
   </activity>';
   return $string;
 }

 public function PostActivity($message) {   
    $details = $this->GetLinkedInDetails(); //own function returning basic details in Stdobject
    $url = 'https://api.linkedin.com/v1/people/~/
       person-activities?oauth2_access_token='.$this->access_token;
    // build your message
    $txt = '<a href="'.$details->siteStandardProfileRequest->url.'">'.$details->firstName.
           ' '.$details->lastName.'</a> '.$message;
    //the below str_replace is only required because the activity contains a <a> tag
    //if you don't have one, just keep the htmlspecialchars
    $txt = str_replace("&","&amp;",htmlspecialchars($txt,ENT_QUOTES,'UTF-8'));
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,str_replace('xml_content',$txt,$this->GetXMLTemplate()));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
    $response = curl_exec($ch);
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    print_r($response);
    echo $http_status;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you use json instead of XML you dont have to deal with XML and encodings. I made a slight improved version of the examples at linkedin, it provides really easy and painless integration, you can check it out if you like: github.com/EJTH/SLinkedIn/blob/master/examples/share.php
LinkedIn recommends (developer.wordpress.com/docs/oauth2) using header-based authorization, as query strings may get logged in unsecure places. So I'd suggest removing oauth2_access_token from the URL and adding 'Authorization: Bearer ' . $this->access_token to the CURLOPT_HTTPHEADER array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.