params = $params; $this->httpClient = $httpClient; $this->requestStack = $requestStack; } /** * @param $listValueId * * @return mixed */ public function get() { try { $client = new Client(['base_uri' => 'https://api.linkedin.com']); $response = $client->request('GET', '/v2/me', [ 'headers' => [ "Authorization" => "Bearer " . $this->params->get('linkedin_token'), ], ]); $result = json_decode($response->getBody()->getContents(), true); } catch(Exception $e) { $result = $e; } return $result; } /** * @param array $profile * * @param string $filePath * * @return array|Exception */ private function uploadFile(array $profile, string $filePath) { try { $client = new Client(['base_uri' => 'https://api.linkedin.com']); $response = $client->request('POST', '/v2/assets', [ 'query' => [ "action" => "registerUpload" ], 'headers' => [ "Authorization" => "Bearer " . $this->params->get('linkedin_token'), 'Content-Type'=> 'multipart/form-data', ], 'json' => [ "registerUploadRequest" => [ "owner" => "urn:li:person:" . $profile['id'], "recipes" => [ "urn:li:digitalmediaRecipe:feedshare-image" ], "serviceRelationships" => [ [ "identifier" => "urn:li:userGeneratedContent", "relationshipType" => "OWNER" ] ], "supportedUploadMechanism" => [ "SYNCHRONOUS_UPLOAD" ] ] ] ]); $result = json_decode($response->getBody()->getContents(), true); $newURL = $result['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl']; $newUrl = explode('https://api.linkedin.com', $newURL); $asset = $result['value']['asset']; $responseFile = $client->request('PUT', $newUrl[1], [ 'headers' => [ "Authorization" => "Bearer " . $this->params->get('linkedin_token'), ], 'body' => fopen($filePath, 'r') ]); $result = $asset; } catch(Exception $e) { $result = $e; } return $result; } public function post(array $profile, string $message, array $filePath) { try { $asset = ""; $medias = []; $content["shareCommentary"]["text"] = $message; $content["shareMediaCategory"] = "NONE"; if(!empty($filePath)){ foreach ($filePath as $path) { $asset = $this->uploadFile($profile, $path); if(!$asset instanceof Exception){ $medias[] = [ "status" => "READY", "media" => $asset, ]; } } if(!empty($medias)){ $content["shareMediaCategory"] = "IMAGE"; $content["media"] = $medias; } } $client = new Client(['base_uri' => 'https://api.linkedin.com']); $respond = $client->request('POST', '/v2/ugcPosts', [ 'headers' => [ "Authorization" => "Bearer " . $this->params->get('linkedin_token'), 'Content-Type'=> 'multipart/form-data', 'X-Restli-Protocol-Version' => "2.0.0" ], 'json' => [ "author" => "urn:li:person:" . $profile['id'], "lifecycleState"=> "PUBLISHED", "specificContent"=> [ "com.linkedin.ugc.ShareContent"=> $content ], "visibility"=> [ "com.linkedin.ugc.MemberNetworkVisibility"=> "PUBLIC" ] ] ]); $result = $respond; } catch (Exception $e) { $result = $e->getMessage(); } return $result; } private function verifAsset(string $asset) { $id = explode('urn:li:digitalmediaAsset:', $asset); $client = new Client(['base_uri' => 'https://api.linkedin.com']); $respond = $client->request('GET', '/v2/assets/'.$id[1], [ 'headers' => [ "Authorization" => "Bearer " . $this->params->get('linkedin_token') ] ]); } }