Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中通过Linkedin API v2在Linkedin公司页面上发布_Php_Linkedin Api_Php 5.3 - Fatal编程技术网

如何在PHP中通过Linkedin API v2在Linkedin公司页面上发布

如何在PHP中通过Linkedin API v2在Linkedin公司页面上发布,php,linkedin-api,php-5.3,Php,Linkedin Api,Php 5.3,我在LinkedIn上创建了一个个人帐户,后来我用相同的个人ID创建了一个公司页面。我想用php创建自动调度程序,这样我就可以在公司页面上发布一些内容 我已经在LinkedIn开发者部分创建了这个应用程序,并且批准了它。我显示该应用程序具有以下权限 r_emailaddress r_basicprofile w_member_social w_organization_social rw_organization_admin 我已经为linkedin创建了一个Oauth,我获得了访问令牌,在获

我在LinkedIn上创建了一个个人帐户,后来我用相同的个人ID创建了一个公司页面。我想用php创建自动调度程序,这样我就可以在公司页面上发布一些内容

我已经在LinkedIn开发者部分创建了这个应用程序,并且批准了它。我显示该应用程序具有以下权限

r_emailaddress
r_basicprofile
w_member_social
w_organization_social
rw_organization_admin
我已经为linkedin创建了一个Oauth,我获得了访问令牌,在获得访问令牌后,我获得了我的linkedin ID,然后我尝试通过API在linkedin上发布内容

当我提出发帖请求时,它会在个人资料上发帖,而不是在它管理的公司页面上

下面是我的配置文件

<?php
        define('CLIENT_ID', 'ID_HERE');
        define('CLIENT_SECRET', 'SECRET_HERE');
        define('REDIRECT_URI', 'call_back_url_here');
        define('SCOPES', 'r_emailaddress,r_basicprofile,w_member_social,w_organization_social,rw_organization_admin');
?>

}

请发布错误,好吗?@Matteo我已经更新了错误问题和一些更新的权限请发布错误吗?@Matteo我已经更新了错误问题和一些更新的权限
<?php
        require_once 'config.php';
        $state = substr(str_shuffle("0123456789abcHGFRlki"), 0, 10);
        $url = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=".CLIENT_ID."&redirect_uri=".REDIRECT_URI."&scope=".SCOPES."&state=".$state;
?>

<a href="<?php echo $url; ?>">Login with LinkedIn</a>
<?php
require_once 'config.php';
require_once '../../../vendor/autoload.php';
use GuzzleHttp\Client;

try {
    $client = new Client(['base_uri' => 'https://www.linkedin.com']);
    $response = $client->request('POST', '/oauth/v2/accessToken', [
        'form_params' => [
                "grant_type" => "authorization_code",
                "code" => $_GET['code'],
                "redirect_uri" => REDIRECT_URI,
                "client_id" => CLIENT_ID,
                "client_secret" => CLIENT_SECRET,
        ],
    ]);
    $data = json_decode($response->getBody()->getContents(), true);
    $access_token = $data['access_token']; // store this token somewhere
    echo $access_token;
} catch(Exception $e) {
    echo $e->getMessage();
}

?>
<?php
require_once 'config.php';
require_once '../../../vendor/autoload.php';
use GuzzleHttp\Client;

$access_token = '';
try {
    $client = new Client(['base_uri' => 'https://api.linkedin.com']);
    $response = $client->request('GET', '/v2/me', [
        'headers' => [
            "Authorization" => "Bearer " . $access_token,
        ],
    ]);
    $data = json_decode($response->getBody()->getContents(), true);
    echo $linkedin_profile_id = $data['id']; // store this id somewhere
} catch(Exception $e) {
    echo $e->getMessage();
}
?>
<?php
require_once '../../../vendor/autoload.php';
use GuzzleHttp\Client;

$link = 'https://www.demo-url.com';
$access_token = '';
// $linkedin_id = 'ubcwiQjr8'; //please note this is demo linkedin ID, but i get some similar ID when i trigger above file
$linkedin_id = '67900000'; //please note this is demo company ID
$body = new \stdClass();
$body->content = new \stdClass();
$body->content->contentEntities[0] = new \stdClass();
$body->text = new \stdClass();
$body->content->contentEntities[0]->thumbnails[0] = new \stdClass();
$body->content->contentEntities[0]->entityLocation = $link;
// $body->content->contentEntities[0]->thumbnails[0]->resolvedUrl = "THUMBNAIL_URL_TO_POST";
$body->content->title = 'Demo title';
$body->owner = 'urn:li:person:'.$linkedin_id;
$body->text->text = 'Somse Dsdfgjjijekdjkjdmo tditle summar1y';
$body_json = json_encode($body, true);

try {
    $client = new Client(['base_uri' => 'https://api.linkedin.com']);
    $response = $client->request('POST', '/v2/shares', [
        'headers' => [
            "Authorization" => "Bearer " . $access_token,
            "Content-Type"  => "application/json",
            "x-li-format"   => "json"
        ],
        'body' => $body_json,
    ]);

    if ($response->getStatusCode() !== 201) {
        echo 'Error: '. $response->getLastBody()->errors[0]->message;
    }

    echo 'Post is shared on LinkedIn successfully';
} catch(Exception $e) {
    echo $e->getMessage(). ' for link '. $link;
}

?>
{
"content": {
  "contentEntities": [
    {
      "entityLocation": "https://www.some-demo-url.com/cyber-security/how-to-keep-my-phone-and-laptop-safe",
      "thumbnails": [
        {}
      ]
    }
  ],
  "title": "Demo Title 1"
},
"owner": "urn:li:organization:739472893749", //organization id
"subject": "Some Subject",
"text": {
  "text": "In the last article, we discussed in detail about passwords and related concepts. Today, we want to take care of the devices that you use to access the internet 1"
}