php LinkedIn REST API -内部服务器错误

m528fe3b  于 2023-05-27  发布在  PHP
关注(0)|答案(4)|浏览(143)

我正在使用LinkedIn REST API向用户时间轴发布更新。几天前,我从LinkedIn收到了一个Internal server error的回复,但代码以前工作过。
PHP:

$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";

$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json'
);

curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

如何修正这个错误?

67up9zun

67up9zun1#

你的代码是无效的PHP(也许是因为你在发布之前做了一些编辑?);将其修改为:

$postTitle = "hello";
$postDesc = "world ";
$postURL = "http://example.com";
$postImage = "http://images.example.com/img/hello.jpg";
$postComment = "foobar";

$oauthToken = "<token>";

$postData = array(
    'visibility' => array('code' => 'connections-only'),
    'content' => array(
        'title' => $postTitle,
        'description' => $postDesc,
        'submitted-url' => $postURL,
        'submitted-image-url' => $postImage
    ),
    'comment' => $postComment
);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json');

curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
    CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

只有$oauthToken设置为有效令牌时才有效。假设你的真实的代码是正确的,剩下的唯一可能性是你的OAuth令牌已经过期,你需要先获得一个新的。通过将CURLOPT_VERBOSE => TRUE添加到cURL选项中,您将了解有关LinkedIn返回的错误的更多信息。

fcwjkofz

fcwjkofz2#

您可以考虑使用LinkedIn PHP SDK(由社区提供):https://github.com/Happyr/LinkedIn-API-client

jv4diomz

jv4diomz3#

我们最近遇到了类似的LinkedIn API问题。最后通过更改URL找到了修复方法。
新URL:“https://api.linkedin.com/v1/people/~/shares
不要在查询字符串中指定'oauth2_access_token',而是将其添加到头中-指定:
“授权”、“承载“+ accessToken。
最后,在请求体参数中,添加要POST的JSON/XML数据

w1e3prcc

w1e3prcc4#

您必须在请求标头中使用身份验证令牌。
这是工作代码。试试看

$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";

$oauthToken = "TokenHere";

$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?format=json');

curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json", "Bearer: ".$oauthToken.""),
CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

相关问题