gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:34:22 25 4
gpt4 key购买 nike

我有一个通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com