【问题标题】:LinkedIn REST API - Company Share | Using JSON or XML, both failLinkedIn REST API - 公司分享 |使用 JSON 或 XML,都失败了
【发布时间】:2017-01-16 10:57:44
【问题描述】:

我在这个问题上花了一天的时间,在 stackoverflow 上阅读了大量的答案,但仍然无法弄清楚。

我想使用 REST API 在 LinkedIn 公司页面上发帖。 我已经有一个通过 OAuth2 API ($secret_token) 检索到的令牌,我还有一个用户拥有管理员权限的公司 ID ($company_ID),我已经通过 cURL 执行了多个“GET”,没有任何问题。

使用控制台 (https://apigee.com/console/linkedin) 我可以做我想做的事。但是一旦翻译成PHP,我就不能让它工作了。

这是我的代码:

// USING XML POST
$postfields = '
<xml>
    <share>
        <visibility>
            <code>anyone</code>
        </visibility>
        <comment>There are a lot of great career opportunities here!</comment>
    </share>
</xml>';

$headers = array('header' => 
    "Authorization: Bearer " .$secret_token. "\r\n".
    'Content-Length: ' . strlen($postfields). "\r\n".
    'Content-Type: text/xml'. "\r\n"
);

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://api.linkedin.com/v1/companies/".$company_id."/shares?format=json",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_HEADER => false,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => $headers, 
    CURLOPT_POSTFIELDS => $postfields
));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
print_r($result);

这显示:

数组([errorCode] => 0 [message] => 无法解析共享文档:错误:意外元素:CDATA [requestId] => IWMFAAIY5E [status] => 400 [timestamp] => 1484563199667)

好的,现在用 JSON 做同样的事情(因为 LinkedIn REST API 允许两者):

// USING JSON POST
$postfields = '{"visibility":  {    "code": "anyone"  },  "comment": "There are a lot of great career opportunities here!"}';

$headers = array('header' => 
    "Authorization: Bearer " .$secret_token. "\r\n".
    'Content-Length: ' . strlen($postfields). "\r\n".
    'Content-Type: application/json'. "\r\n".
    "x-li-format: json". "\r\n"
);

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://api.linkedin.com/v1/companies/".$company_id."/shares?format=json",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_HEADER => false,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => $headers, 
    CURLOPT_POSTFIELDS => $postfields
));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
print_r($result);

现在显示:

Array ( [errorCode] => 0 [message] => 无法解析 Json 正文:意外字符 ('C' (code 67)):预期有效值(数字、字符串、数组、对象、'true '、'false' 或 'null')在 [Source: java.io.StringReader@7e6528c1; line: 1, column: 2] [requestId] => HIRNMKAQTC [status] => 400 [timestamp] => 1484564171949)

有什么想法吗?

【问题讨论】:

  • 到目前为止没有答案 - 是因为没有人知道线索还是我的问题中缺少一些重要信息?
  • 应该是:

标签: php json xml curl linkedin


【解决方案1】:

在您的 $headers 变量上,删除结尾的 ."\r\n"

$headers = array('header' => 
    "Authorization: Bearer " .$secret_token. "\r\n".
    'Content-Length: ' . strlen($postfields). "\r\n".
    'Content-Type: application/json'. "\r\n".
    "x-li-format: json"
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-02
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多