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

标签 php api linkedin

我有一个通过 linkedin api 对用户进行身份验证的应用程序:

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

  • 是否可以向部分应用程序用户发送消息? (即:黄金成员(member)等。您可以假设我将所有 linkedin ID 存储在某处)

找了半天还是没找到/

最佳答案

像这样

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

关于php - 我如何使用 linkedin api 发送消息/通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6499259/

相关文章:

linkedin - LinkedIn API 返回的时间戳是什么格式?

java - Cleo(linkedin 的自动完成解决方案)适用于数十亿个元素吗?

php - 在 MySQL Linux 服务器上安装 PDO 驱动程序

php - xdebug 和 CentOS 的问题

java - 我如何在 APK 文件中保护此 API

python - 将嵌套字典展平为键 :value pair 列表

PHP - 删除粗体文本

php - 在 MySQL 中恢复授予权限语句

api - TeamCity REST API : Who broke the build?

javascript - Linkedin Api 如何通过人员搜索获取群组详细信息