加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:12:22 所属栏目: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/xmlrn"
                    . "Content-Length: " . strlen($body) . "rn",'content' => ($body)
            )
        )
    );


    // Hocus Pocus
    $fp = fopen($url,'r',false,$context);
    $response = file_get_contents($url,$context);
    $result =json_decode($response,true);

    return $result;}
message('Subject','body',array('id'));

从Code Sample获取函数

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读