laravel 如何通过 Linkedin API 将帖子分享到公司页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23567962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:30:15  来源:igfitidea点击:

How to share post to company page via Linkedin API?

phpapilaravellinkedin

提问by lieroes

I can't do anything without your help.

没有你的帮助,我什么也做不了。

My task is share post to LinkedIn on company page via LinkedIn API. GET request works fine for me, but when i try create POST request i get this error message:

我的任务是通过 LinkedIn API 在公司页面上将帖子分享到 LinkedIn。GET 请求对我来说工作正常,但是当我尝试创建 POST 请求时,我收到此错误消息:

file_get_contents(https://api.linkedin.com/v1/companies/3723615/shares?visbility%5Bcode%5D=anyone&comment=test+comment): failed to open stream: HTTP request failed! HTTP/1.0 400 request#no_content_length

So.. it's a part of my code (this code i get from official docs):

所以..它是我代码的一部分(我从官方文档中获得了这个代码):

public function index() {
    ....
    // This GET request works fine
    $company = $this->companyInfo('GET', '/v1/companies/universal-name=ovdwebdev');
    print_r($company);
    // This one give me error
    $post = $this->companyPost('POST', '/v1/companies/3723615/shares');
    exit;
}

private function companyInfo($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);
}

private function getAuthorizationCode($api_key, $scope, $redirect_uri) {
    $params = array('response_type' => 'code',
        'client_id' => $api_key,
        'scope' => $scope,
        'state' => uniqid('', true), // unique long string
        'redirect_uri' => $redirect_uri,
    );

    // Authentication request
    $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);

    // Needed to identify request when it returns to us
    $_SESSION['state'] = $params['state'];

    // Redirect user to authenticate
    header("Location: $url");
    exit;
}

回答by ???

file_get_contents using URL

file_get_contents 使用 URL

See the docs;

查看文档

A URL can be used as a filename with this function if the fopenwrappers have been enabled. See fopen()for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

如果已启用fopen包装器,则可以将 URL 用作此函数的文件名。有关如何指定文件名的更多详细信息,请参阅fopen()。请参阅支持的协议和包装器以获取有关各种包装器具有哪些功能的信息、有关其使用的说明以及有关它们可能提供的任何预定义变量的信息的链接。

However,

然而,

With a POSTrequest, you'd need to POSTsome data. With REST API, this is usually done in the body.

对于POST请求,您需要POST一些数据。使用 REST API,这通常在主体中完成。

Your error; no_content_length

你的错误; no_content_length

Perhaps look into cURL- Here's a quick sample I put together for you- It's not tested, so read their docs.

也许看看cURL-这是我为你整理的一个快速示例- 它没有经过测试,所以请阅读他们的文档

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$strShare = <<<XML
<?xml version="1.0" encoding="UTF-8"?> 
<share>  
  <comment>Check out the LinkedIn Share API!</comment>  
  <content> 
     <title>LinkedIn Developers Documentation On Using the Share API</title>  
    <description>Leverage the Share API to maximize engagement on user-generated content on LinkedIn</description> 
    <submitted-url>https://developer.linkedin.com/documents/share-api</submitted-url> 
    <submitted-image-url>http://m3.licdn.com/media/p/3/000/124/1a6/089a29a.png</submitted-image-url> 
  </content> 
  <visibility> 
    <code>anyone</code> 
  </visibility>  
</share>
XML;

$objCurl = curl_init();
curl_setopt($objCurl, CURLOPT_URL, "http://api.linkedin.com/v1/people/~/shares");
curl_setopt($objCurl, CURLOPT_HEADER, 1);
curl_setopt($objCurl, CURLOPT_POST, 1);
curl_setopt($objCurl, CURLOPT_POSTFIELDS, $strShare);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                        'Content-Type: application/xml',
                                        'Connection: Keep-Alive'
                                        ));
curl_setopt($objCurl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($objCurl);
curl_close($objCurl);

echo '<pre>';
echo print_r( $response, true );
echo '</pre>';

Perhaps you could look into SOAPseeming as it's XML

也许您可以将SOAP视为 XML

Hope that helps!

希望有帮助!