使用Oauth2访问令牌在PHP中将网络更新发布到LinkedIn
LinkedIn REST API的许多代码示例和工具都假设您已使用Oauth1进行标识,并因此获得了成员令牌和成员机密 现在,LinkedIn提供的示例代码如下: 允许获取Oauth2访问令牌 一旦拥有了access_令牌,就可以使用代码示例中提供的fetch函数进行查询并获取成员的详细信息使用Oauth2访问令牌在PHP中将网络更新发布到LinkedIn,php,oauth-2.0,linkedin,Php,Oauth 2.0,Linkedin,LinkedIn REST API的许多代码示例和工具都假设您已使用Oauth1进行标识,并因此获得了成员令牌和成员机密 现在,LinkedIn提供的示例代码如下: 允许获取Oauth2访问令牌 一旦拥有了access_令牌,就可以使用代码示例中提供的fetch函数进行查询并获取成员的详细信息 function fetch($method, $resource, $body = '') { $params = array('oauth2_access_token' => $_SESS
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,
)
)
);
// Hocus Pocus
$response = file_get_contents($url, false, $context);
// Native PHP object, please
return json_decode($response);
}
好的,太好了,这个很好用。但是prodived fetch函数假定body为NULL。
但是要使用发布网络更新,需要在正文中传递一些XML。我尝试了很多方法,发现了几十个使用Oauth 1.0成员令牌和成员机密的示例。但我还没有找到任何在您只有Oauth2访问令牌时有效的解决方案。因此,问题是:
这个fetch函数需要做哪些更改才能在查询中传递XML正文?最后,我找到了答案
function GetXMLTemplate(){
string = '<?xml version="1.0" encoding="UTF-8"?><activity locale="en_US">
<content-type>linkedin-html</content-type>
<body>xml_content</body>
</activity>';
return $string;
}
public function PostActivity($message) {
$details = $this->GetLinkedInDetails(); //own function returning basic details in Stdobject
$url = 'https://api.linkedin.com/v1/people/~/
person-activities?oauth2_access_token='.$this->access_token;
// build your message
$txt = '<a href="'.$details->siteStandardProfileRequest->url.'">'.$details->firstName.
' '.$details->lastName.'</a> '.$message;
//the below str_replace is only required because the activity contains a <a> tag
//if you don't have one, just keep the htmlspecialchars
$txt = str_replace("&","&",htmlspecialchars($txt,ENT_QUOTES,'UTF-8'));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,str_replace('xml_content',$txt,$this->GetXMLTemplate()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($response);
echo $http_status;
}
函数GetXMLTemplate(){
字符串='.$message;
//以下str_replace是必需的,因为活动包含标记
//如果没有,请保留htmlspecialchars
$txt=str_replace(&,“&;”,htmlspecialchars($txt,ENT_引号,'UTF-8');
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,str_replace('xml_content',$txt,$this->GetXMLTemplate());
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type:application/xml'));
$response=curl\u exec($ch);
$http\u status=curl\u getinfo($ch,CURLINFO\u http\u代码);
卷曲关闭($ch);
打印(回复);
echo$http_状态;
}
如果使用json而不是XML,则不必处理XML和编码。我在linkedin上做了一个稍微改进的示例版本,它提供了非常简单和无痛的集成,如果您愿意,您可以查看它:linkedin建议()使用基于头的授权,因为查询字符串可能会登录到不安全的位置。因此,我建议从URL中删除
oauth2\u访问\u令牌
,并添加'Authorization:Bearer'$此->访问\u令牌
到CURLOPT\u HTTPHEADER
数组。