how do i send a message/notification with the linkedin api?
i have an application that authenticates users through the 开发者_开发知识库linkedin api:
is it possible for an application to send messages to all users who authorized it? (ie: the app's system notifications)
is it possible to send a message to a subset of the application users? (ie: gold members etc. you can assume i have all the linkedin IDs stored somewhere)
i've been looking for a while, and can't find anything/
Something like this
function message($subject, $body, $recipients)
{
if (!is_array($recipients)) {
throw new Exception('Recipients must be suplied as an array');
}
// Start document
$xml = new DOMDocument('1.0', 'utf-8');
// Create element for recipients and add each recipient as a node
$elemRecipients = $xml->createElement('recipients');
foreach ($recipients as $recipient) {
// Create person node
$person = $xml->createElement('person');
$person->setAttribute('path', '/people/' . (string) $recipient);
// Create recipient node
$elemRecipient = $xml->createElement('recipient');
$elemRecipient->appendChild($person);
// Add recipient to recipients node
$elemRecipients->appendChild($elemRecipient);
}
// Create mailbox node and add recipients, body and subject
$elemMailbox = $xml->createElement('mailbox-item');
$elemMailbox->appendChild($elemRecipients);
$elemMailbox->appendChild($xml->createElement('body', ($body)));
$elemMailbox->appendChild($xml->createElement('subject', ($subject)));
// Append parent node to document
$xml->appendChild($elemMailbox);
$response = fetch('POST','/v1/people/~/mailbox', $xml->saveXML());
return ($response);
}
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,
'header'=> "Content-Type:text/xml\r\n"
. "Content-Length: " . strlen($body) . "\r\n",
'content' => ($body)
)
)
);
// Hocus Pocus
$fp = fopen($url, 'r', false, $context);
$response = file_get_contents($url, false, $context);
$result =json_decode($response,true);
return $result;}
message('Subject', 'body', array('id'));
function fetch from Code Sample
The only messaging supported by the API is via the Messaging API, which only allows for messages to be sent from one connection to another... so in theory, you (as in you the developer, not the application itself, as it has no connections) could send a message to any of your applications user's that you also happen to be connected to in some way. The Messaging API is pretty clear though that messages must be triggered by a specific action, and the maximum number of recipients is 10.
So the short answer is, not possible, although the above may be a bit of a workaround. An alternative would be to ask the user directly for their email address once they have completed the LI process and then you can contact them as you wish without running into API limits/restrictions.
精彩评论