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

时间:2014-05-09 15:04:40

标签: php api laravel linkedin

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

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

1 个答案:

答案 0 :(得分:0)

使用网址

的file_get_contents

the docs;

  

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

然而,

如果发出POST请求,您需要POST部分数据。使用REST API,通常在正文中完成。

你的错误; no_content_length

或许查看cURL - 这是我为您准备的快速示例 - 未经过测试,请阅读docs。< / p>

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

希望有所帮助!