【问题标题】:How to share post to company page via Linkedin API?如何通过 Linkedin API 将帖子分享到公司页面?
【发布时间】:2014-06-27 09:23:34
【问题描述】:

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

我的任务是通过 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

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

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;
}

【问题讨论】:

    标签: php api laravel linkedin


    【解决方案1】:

    file_get_contents 使用 URL

    the docs

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

    然而,

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

    你的错误; no_content_length

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

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

    也许您可以查看 SOAP 看起来像是 XML

    希望有帮助!

    【讨论】:

    • 这个 API 让我抓狂。现在我收到代码 401“未知身份验证方案”的错误,我必须使用 OAuth 进行身份验证?
    • 是的。看看developer.linkedin.com/documents/authentication 的认证步骤
    • 我可以得到我的公司头衔等等……这意味着我已经通过了身份验证。我的 access_token 在 "curl_setopt($objCurl, CURLOPT_URL, "api.linkedin.com/v1/companies/3723615/…" 中是 "XXXXXX",对吗?现在我收到此错误消息:“无法解析共享文档:错误:null 后文件意外结束”。在我的应用程序我使用你的代码示例..
    • 今晚我会好好看看,然后写一个分步指南:) 完成后我会在今晚晚些时候通知你。
    • 嘿.. :) 你没有忘记我吗? :)
    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多