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

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

>应用程序是否可以向授权它的所有用户发送消息? (即:应用程序的系统通知)
>是否可以向应用程序用户的子集发送消息? (即:黄金会员等你可以假设我已经存储了所有的地址)

我一直在寻找一段时间,找不到任何东西/

最佳答案 像这样的东西

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获取函数

点赞