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

这段代码展示了如何创建一个XML结构来表示邮件的收件人、主题和正文,并使用PHP通过LinkedIn API发送。它首先检查收件人是否为数组,然后构建XML文档,包括邮件项、收件人节点和每个收件人的详细信息。接着,它通过POST请求将XML文档发送到LinkedIn API,返回响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

像这样的东西

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值