auth->setOAuthToken($socialInfo['token']); try { $pin->pins->create([ "note" => $content, "image_url" => $imgPath, "board" => $board, 'link' => $url ]); } catch (\Throwable $throwable) { return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)]; } return [SocialPublish::STATUS_SUCCESS, '']; } public function linkedIn($content, $orgId) { if (empty(trim($orgId))) { return [SocialPublish::STATUS_NO_NEED, 'linkedIn orgId 不存在']; } $socialInfo = Social::query()->where(['type' => Social::LINKED, 'site_id' => 0])->first(); if (empty($socialInfo->token)) { return [SocialPublish::STATUS_FAILURE, 'linkedIn token 不存在']; } $content = mb_substr($content, 0, 450); try { LinkedinShare::shareNoneCompany($socialInfo->token, $content, $orgId, 'accessToken'); } catch (\Throwable $throwable) { Log::warning(sprintf('linedIn:%s', var_export($throwable->getMessage(), 1))); return [SocialPublish::STATUS_FAILURE, var_export(substr($throwable->getMessage(), 0, 200), 1)]; } return [SocialPublish::STATUS_SUCCESS, '']; } public function twitter($imgPath, $url, $content, $socialInfo) { if (empty($socialInfo['token']) || empty($socialInfo['token_secret'])) { return [SocialPublish::STATUS_FAILURE, 'twitter的token或secret 不存在']; } $newContent = sprintf('%s %s', $url, $content); $content = mb_substr(trim($newContent), 0, 280); Twitter::reconfig([ 'consumer_key' => config('services.twitter.client_id'), 'consumer_secret' => config('services.twitter.client_secret'), 'token' => $socialInfo['token'], 'secret' => $socialInfo['token_secret'] ]); try { if ($imgPath) { $imgPath = public_path($imgPath); $uploaded_media = Twitter::uploadMedia(['media' => File::get($imgPath)]); $addition = ['media_ids' => $uploaded_media->media_id_string]; } Twitter::postTweet(['status' => $content] + ($addition ?? [])); // ILog::query()->create([ // 'type' => 'twitter', // 'content' => var_export($result, 1) // ]); } catch (\Throwable $throwable) { return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)]; } return [SocialPublish::STATUS_SUCCESS, '']; } public function facebookTrue($content, $url, $siteInfo) { if (empty($siteInfo['facebook_page']) || empty($siteInfo['facebook_page_token'])) { return [SocialPublish::STATUS_FAILURE, '主页信息不存在']; } try { $fb = new Facebook([ 'app_id' => env('FACEBOOK_CLIENT_ID'), 'app_secret' => env('FACEBOOK_CLIENT_SECRET'), 'default_access_token' => $siteInfo['facebook_page_token'], // optional ]); $linkData = [ 'link' => $url, 'message' => $content, ]; // $pageId = '2822252084511458'; $pageId = $siteInfo['facebook_page']; $response = $fb->post(sprintf('/%s/feed', $pageId), $linkData); ILog::query()->create([ 'type' => 'facebook', 'content' => var_export($response->getBody(), 1) ]); } catch (\Throwable $throwable) { return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)]; } return [SocialPublish::STATUS_SUCCESS, '']; } /** * 获取图片 * @param $img * @return string */ public function getImg($img) { if (!$img) { return false; } try { $pathInfo = pathinfo($img); $imgContent = file_get_contents($img); $filePath = sprintf('social/%s.%s', Str::random(40), $pathInfo['extension'] ?? ''); Storage::disk('public')->put($filePath, $imgContent, 'public'); return sprintf('/storage/%s', $filePath); } catch (\Throwable $throwable) { Log::warning('getImg:' . var_export($throwable->getMessage(), 1)); return false; } } /** * 获取链接 * @param $siteId * @param $remoteContentId * @return string */ protected function getUrl($siteId, $remoteContentId) { $site = Site::query()->with('server')->select(['id', 'server_id', 'database', 'domain'])->where(['id' => $siteId])->first(); if (empty($site->database) || empty($site->server)) { Log::warning('getUrl: 数据库或服务器信息不存在'); return false; } $config = [ 'connection_name' => sprintf('connection_name_%s', $site->id), 'host' => $site->server->server_ip, 'port' => 3306, 'database' => $site->database, 'username' => $site->server->mysql_user_name, 'password' => $site->server->mysql_passwd, ]; config_connection($config); try { $record = DB::connection($config['connection_name'])->table('content')->where(['id' => $remoteContentId])->first(); if ($record) { $url = sprintf('http://%s/%s.html', $site->domain, $record->uri ?? ''); return $url; } Log::warning('getUrl: 远程记录信息不存在'); } catch (\Throwable $throwable) { Log::warning('getUrl:' . var_export($throwable->getMessage(), 1)); } return false; } // public function articlePublish($article, &$sitesMap) // { // $img = sprintf('%s%s', config('app.admin_url'), $article->thumb); // $imgPath = $this->getImg($img); // // $url = $this->getUrl($article->site_id, $article->remote_content_id); // if (!$url) { // return false; // } // // $content = str_replace('
', PHP_EOL, html_entity_decode(strip_tags($article->translate_content, '
'))); // //// $linkedIn = $this->linkedIn($content, $url); // $pin = $this->pin($imgPath, $content); // $twitter = $this->twitter($imgPath, $content); // // $pageId = $sitesMap[$article->site_id]['facebook_page'] ?? null; // $facebook = $this->facebookTrue($content, $url, $pageId); // return compact('linkedIn', 'twitter', 'pin', 'facebook'); // } public function publish($record, $sitesMap) { $socialInfo = Social::query()->where(['site_id' => $record->site_id])->get()->keyBy('type')->toArray(); $imgPath = $this->getImg($record->media); $content = str_replace('
', PHP_EOL, html_entity_decode(strip_tags($record->content, '
'))); list($linkedInStatus, $linkedErr) = $this->linkedIn($content, $record->linkedin_orgid); list($pinStatus, $pinErr) = $this->pin($imgPath, $record->url, $content, $record->pin_board, ($socialInfo[Social::PIN] ?? null)); list($twStatus, $twErr) = $this->twitter($imgPath, $record->url, $content, ($socialInfo[Social::TWITTER] ?? null)); // $siteInfo = $sitesMap[$record->site_id] ?? null; // list($fbStatus, $fbErr) = $this->facebookTrue($content, $record->url, $siteInfo); return [ ['pin' => $pinErr, 'twitter' => $twErr, 'linkedIn' => $linkedErr], ['pin' => $pinStatus, 'twitter' => $twStatus,'linkedIn' => $linkedInStatus] ]; } }