如何使用linkedin api发送消息/通知?

时间:2011-06-27 21:39:45

标签: php api linkedin

我有一个通过linkedin API验证用户的应用程序:

  • 应用程序是否可以向授权它的所有用户发送消息? (即:应用程序的系统通知)

  • 是否可以向应用程序用户的子集发送消息? (即:黄金会员等,您可以假设我已将所有的商店ID存储在某处)

我一直在寻找,但找不到任何东西/

2 个答案:

答案 0 :(得分:2)

像这样的东西

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'));

Code Sample

获取功能

答案 1 :(得分:0)

API支持的唯一消息传递是通过Messaging API,它只允许将消息从一个连接发送到另一个连接......所以理论上,你(就像在你的开发人员,而不是应用程序)本身,因为它没有连接)可以向您的任何应用程序用户发送消息,您也恰好以某种方式连接到该用户。 Messaging API非常清楚,但邮件必须由特定操作触发,并且最大收件人数为10。

所以简短的答案是不可能的,尽管上面的解决方案可能有些变化。另一种方法是在用户完成LI流程后直接询问用户的电子邮件地址,然后您可以根据需要联系他们,而不会遇到API限制/限制。