FacebookService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Services\Smm;
  3. use App\Services\Contracts\SmmPlatformInterface;
  4. use Illuminate\Http\Request;
  5. use Facebook\Facebook;
  6. class FacebookService implements SmmPlatformInterface
  7. {
  8. public $fb = null;
  9. public $pageAccessToken = null;
  10. public function __construct() {
  11. $this->fb = new Facebook([
  12. 'app_id' => env('SSM_FACEBOOK_APP_ID'), // 替换为您的App ID
  13. 'app_secret' => env('SSM_FACEBOOK_APP_SECRET'), // 替换为您的App Secret
  14. 'default_graph_version' => 'v22.0', // 使用当前版本
  15. ]);
  16. //$this->pageAccessToken = $pageAccessToken;
  17. }
  18. /*
  19. * OAuth 2.0 授权登录
  20. * 返回授权地址:https://example.com/fb-callback.php
  21. */
  22. public function login()
  23. {
  24. // 实现Facebook登录逻辑
  25. $helper = $this->fb->getRedirectLoginHelper();
  26. $permissions = ['public_profile','email'];
  27. $loginUrl = $helper->getLoginUrl(env('DIST_SITE_URL').'/open/callback/facebook', $permissions);
  28. return ['status'=>true, 'data' => ['url'=>$loginUrl]];
  29. }
  30. /*
  31. * OAuth 2.0 授权回调
  32. * 授权成功后,得到access_token,refresh_token等信息, 保存到数据库中
  33. * 授权成功后,返回回调需要的数据
  34. */
  35. public function loginCallback(Request $request)
  36. {
  37. dd($_GET);
  38. // 实现Facebook回调处理
  39. $helper = $this->fb->getRedirectLoginHelper();
  40. // Validate the state parameter
  41. try {
  42. $accessToken = $helper->getAccessToken();
  43. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  44. return ['status' => false, 'data' => 'Graph API错误:' . $e->getMessage()];
  45. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  46. return ['status' => false, 'data' => 'SDK错误:' . $e->getMessage()];
  47. }
  48. if (isset($accessToken)) {
  49. // 可选:将短期令牌转换为长期令牌(有效期约60天)
  50. $oAuth2Client = $this->fb->getOAuth2Client();
  51. $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  52. $useriInfo = $this->getFacebookUser($longLivedAccessToken);
  53. return ['status' => true, 'data' => ['access_token' => $longLivedAccessToken,'user_name'=>$useriInfo['name'],'user_id'=>$useriInfo['id']]];
  54. } else {
  55. return ['status' => false, 'data' => '无法获取访问令牌'];
  56. }
  57. }
  58. public function postImage($message,$imagePath) {
  59. try {
  60. $response = $this->fb->post(
  61. '/me/photos',
  62. [
  63. 'source' => $this->fb->fileToUpload($imagePath), // 上传图片文件
  64. 'message' => $message, // 添加描述
  65. ],
  66. $this->userAccessToken
  67. );
  68. $graphNode = $response->getGraphNode();
  69. return ['status' => true, 'data' => ['id' => $graphNode['id']]];
  70. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  71. $errorMsg = 'Graph API错误:' . $e->getMessage();
  72. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  73. $errorMsg = 'SDK错误:' . $e->getMessage();
  74. }
  75. return ['status'=>false,'data' => $errorMsg];
  76. }
  77. public function postVideo($message,$videoPath) {
  78. try {
  79. $response = $this->fb->post(
  80. '/me/videos',
  81. [
  82. 'source' => $this->fb->fileToUpload($videoPath), // 上传视频文件
  83. 'title' => $message, // 添加标题
  84. 'description' => '', // 添加描述
  85. ],
  86. $this->userAccessToken
  87. );
  88. $graphNode = $response->getGraphNode();
  89. return ['status' => true, 'data' => ['id' => $graphNode['id']]];
  90. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  91. $errorMsg = 'Graph API错误:' . $e->getMessage();
  92. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  93. $errorMsg = 'SDK错误:' . $e->getMessage();
  94. }
  95. return ['status'=>false,'data' => $errorMsg];
  96. }
  97. public function getComments($postId) {
  98. }
  99. public function replyToComment($commentId) {
  100. }
  101. public function deleteComment($commentId) {
  102. }
  103. /**
  104. * 获取Facebook用户信息
  105. * @param string $accessToken
  106. * @return array [
  107. * 'name' => string,
  108. * 'id' => string,
  109. * 'error' => string|null
  110. * ]
  111. */
  112. public function getFacebookUser($accessToken)
  113. {
  114. try {
  115. // 验证并设置访问令牌
  116. $this->fb->setDefaultAccessToken($accessToken);
  117. // 发送请求获取用户信息
  118. $response = $this->fb->get('/me?fields=name,id');
  119. $userNode = $response->getGraphUser();
  120. return [
  121. 'name' => $userNode->getName(),
  122. 'id' => $userNode->getId(),
  123. 'error' => null
  124. ];
  125. } catch (FacebookResponseException $e) {
  126. // API 响应错误处理
  127. return [
  128. 'name' => null,
  129. 'id' => null,
  130. 'error' => 'Graph API Error: ' . $e->getMessage()
  131. ];
  132. } catch (FacebookSDKException $e) {
  133. // SDK 错误处理
  134. return [
  135. 'name' => null,
  136. 'id' => null,
  137. 'error' => 'SDK Error: ' . $e->getMessage()
  138. ];
  139. } catch (Exception $e) {
  140. // 其他错误处理
  141. return [
  142. 'name' => null,
  143. 'id' => null,
  144. 'error' => 'General Error: ' . $e->getMessage()
  145. ];
  146. }
  147. }
  148. }