1
votes

Yo!

I'm trying to use the linkedin invitation api to allow users to conncect on linkedin from my application using email-addresses. I am able to find people, access the api and so on. I can't get the invites to work though. I am using php (Laravel).

I based myself on the example from the linkedin documentation ( Linkedin Invite API ). I send my data in a post using JSON (that contains the same info as their example).

I ask permission to use w_messages, the post works and my variables contain the correct information. I get a Internal Server error as a result.

$data = array(
        "recipients" => array(
                "values" => array(
                    "person" => array(
                        "_path" => "/people/email=".$email,
                        "first-name" => $firstname,
                        "last-name" => $lastname
                        )
                    )
            ),
        "subject" => "Bla",
        "body"=> "BlaBLa", 
        "item-content" => array(
                "invitation-request" => array(
                        "connect-type" => "friend"
                    )
            )
        ); 

    $dataString = json_encode($data);

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => "Content-type: application/json\r\n".
                          "Connection: close\r\n" .
                          "Content-length: " . strlen($dataString) . "\r\n",
            'content' => $dataString
        )
    );

    $params = array('oauth2_access_token' => Session::get('access_token'),
        'format' => 'json'
        );


    $url = "https://api.linkedin.com/v1/people/~/mailbox".'?' . http_build_query($params);
    $context = stream_context_create($opts);
    $result = file_get_contents($url, false, $context);
    Log::info($result);

    return Response::json(array("invite" => "sent"));

I assume I'm doing something wrong but don't really know where to look.

1

1 Answers

0
votes

Looks like you doing this manually, have you tried using a tried & tested third party library like simple-linkedinphp - A PHP-based wrapper for the LinkedIn API.

https://code.google.com/p/simple-linkedinphp/wiki/Reference

Usage:

// Connect
$API_CONFIG = array(
  'appKey'       => '<your application key here>',
  'appSecret'    => '<your application secret here>',
  'callbackUrl'  => NULL 
);
$linkedin = new LinkedIn($API_CONFIG);

// Send Invite
$linkedin->invite($method, $recipient, $subject, $body, $type = 'friend');

Doc: https://code.google.com/p/simple-linkedinphp/wiki/Reference