|
@@ -1,11 +1,25 @@
|
|
|
<?php
|
|
|
|
|
|
namespace App\Services\Smm;
|
|
|
-use App\Services\Smm\Contracts\SmmPlatformInterface;
|
|
|
+use App\Services\Contracts\SmmPlatformInterface;
|
|
|
use Illuminate\Http\Request;
|
|
|
+use Facebook\Facebook;
|
|
|
+
|
|
|
|
|
|
class FacebookService implements SmmPlatformInterface
|
|
|
{
|
|
|
+ public $fb = null;
|
|
|
+ public $pageAccessToken = null;
|
|
|
+
|
|
|
+ public function __construct() {
|
|
|
+ $this->fb = new Facebook([
|
|
|
+ 'app_id' => env('SSM_FACEBOOK_APP_ID'),
|
|
|
+ 'app_secret' => env('SSM_FACEBOOK_APP_SECRET'),
|
|
|
+ 'default_graph_version' => 'v12.0',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* OAuth 2.0 授权登录
|
|
|
* 返回授权地址:https:
|
|
@@ -13,7 +27,11 @@ class FacebookService implements SmmPlatformInterface
|
|
|
public function login()
|
|
|
{
|
|
|
|
|
|
-
|
|
|
+ $helper = $this->fb->getRedirectLoginHelper();
|
|
|
+ $permissions = ['publish_pages'];
|
|
|
+ $distSiteUrl = env('DIST_SITE_URL');
|
|
|
+ $loginUrl = $helper->getLoginUrl($distSiteUrl.'/callback/facebook', $permissions);
|
|
|
+ return ['status'=>true, 'data' => ['login_url'=>$loginUrl]];
|
|
|
}
|
|
|
|
|
|
|
|
@@ -24,8 +42,77 @@ class FacebookService implements SmmPlatformInterface
|
|
|
public function loginCallback(Request $request)
|
|
|
{
|
|
|
|
|
|
+ $helper = $this->fb->getRedirectLoginHelper();
|
|
|
+ try {
|
|
|
+ $accessToken = $helper->getAccessToken();
|
|
|
+ } catch (Facebook\Exceptions\FacebookResponseException $e) {
|
|
|
+ return ['status' => false, 'data' => 'Graph API错误:' . $e->getMessage()];
|
|
|
+ } catch (Facebook\Exceptions\FacebookSDKException $e) {
|
|
|
+ return ['status' => false, 'data' => 'SDK错误:' . $e->getMessage()];
|
|
|
+ }
|
|
|
+ if (isset($accessToken)) {
|
|
|
+
|
|
|
+ $oAuth2Client = $this->fb->getOAuth2Client();
|
|
|
+ $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
|
|
|
+ return ['status' => true, 'data' => ['access_token' => $longLivedAccessToken]];
|
|
|
+ } else {
|
|
|
+ return ['status' => false, 'data' => '无法获取访问令牌'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function postImage($message,$imagePath) {
|
|
|
+ try {
|
|
|
+ $response = $this->fb->post(
|
|
|
+ '/me/photos',
|
|
|
+ [
|
|
|
+ 'source' => $this->fb->fileToUpload($imagePath),
|
|
|
+ 'message' => $message,
|
|
|
+ ],
|
|
|
+ $this->userAccessToken
|
|
|
+ );
|
|
|
+ $graphNode = $response->getGraphNode();
|
|
|
+ return ['status' => true, 'data' => ['id' => $graphNode['id']]];
|
|
|
+ } catch (Facebook\Exceptions\FacebookResponseException $e) {
|
|
|
+ $errorMsg = 'Graph API错误:' . $e->getMessage();
|
|
|
+ } catch (Facebook\Exceptions\FacebookSDKException $e) {
|
|
|
+ $errorMsg = 'SDK错误:' . $e->getMessage();
|
|
|
+ }
|
|
|
+ return ['status'=>false,'data' => $errorMsg];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function postVideo($message,$videoPath) {
|
|
|
+ try {
|
|
|
+ $response = $this->fb->post(
|
|
|
+ '/me/videos',
|
|
|
+ [
|
|
|
+ 'source' => $this->fb->fileToUpload($videoPath),
|
|
|
+ 'title' => $message,
|
|
|
+ 'description' => '',
|
|
|
+ ],
|
|
|
+ $this->userAccessToken
|
|
|
+ );
|
|
|
+ $graphNode = $response->getGraphNode();
|
|
|
+ return ['status' => true, 'data' => ['id' => $graphNode['id']]];
|
|
|
+ } catch (Facebook\Exceptions\FacebookResponseException $e) {
|
|
|
+ $errorMsg = 'Graph API错误:' . $e->getMessage();
|
|
|
+ } catch (Facebook\Exceptions\FacebookSDKException $e) {
|
|
|
+ $errorMsg = 'SDK错误:' . $e->getMessage();
|
|
|
+ }
|
|
|
+ return ['status'=>false,'data' => $errorMsg];
|
|
|
}
|
|
|
|
|
|
+ public function getComments($postId) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public function replyToComment($commentId) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public function deleteComment($commentId) {
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|