FacebookService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. $distSiteUrl = env('DIST_SITE_URL');
  28. $loginUrl = $helper->getLoginUrl(env('DIST_SITE_URL').'/open/callback/facebook', $permissions);
  29. return ['status'=>true, 'data' => ['url'=>$loginUrl]];
  30. }
  31. /*
  32. * OAuth 2.0 授权回调
  33. * 授权成功后,得到access_token,refresh_token等信息, 保存到数据库中
  34. * 授权成功后,返回回调需要的数据
  35. */
  36. public function loginCallback(Request $request)
  37. {
  38. // 实现Facebook回调处理
  39. $helper = $this->fb->getRedirectLoginHelper();
  40. try {
  41. $accessToken = $helper->getAccessToken();
  42. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  43. return ['status' => false, 'data' => 'Graph API错误:' . $e->getMessage()];
  44. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  45. return ['status' => false, 'data' => 'SDK错误:' . $e->getMessage()];
  46. }
  47. if (isset($accessToken)) {
  48. // 可选:将短期令牌转换为长期令牌(有效期约60天)
  49. $oAuth2Client = $this->fb->getOAuth2Client();
  50. $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  51. $useriInfo = $this->getFacebookUser($longLivedAccessToken);
  52. return ['status' => true, 'data' => ['access_token' => $longLivedAccessToken,'user_name'=>$useriInfo['name'],'user_id'=>$useriInfo['id']]];
  53. } else {
  54. return ['status' => false, 'data' => '无法获取访问令牌'];
  55. }
  56. }
  57. public function postImage($message,$imagePath) {
  58. try {
  59. $response = $this->fb->post(
  60. '/me/photos',
  61. [
  62. 'source' => $this->fb->fileToUpload($imagePath), // 上传图片文件
  63. 'message' => $message, // 添加描述
  64. ],
  65. $this->userAccessToken
  66. );
  67. $graphNode = $response->getGraphNode();
  68. return ['status' => true, 'data' => ['id' => $graphNode['id']]];
  69. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  70. $errorMsg = 'Graph API错误:' . $e->getMessage();
  71. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  72. $errorMsg = 'SDK错误:' . $e->getMessage();
  73. }
  74. return ['status'=>false,'data' => $errorMsg];
  75. }
  76. public function postVideo($message,$videoPath) {
  77. try {
  78. $response = $this->fb->post(
  79. '/me/videos',
  80. [
  81. 'source' => $this->fb->fileToUpload($videoPath), // 上传视频文件
  82. 'title' => $message, // 添加标题
  83. 'description' => '', // 添加描述
  84. ],
  85. $this->userAccessToken
  86. );
  87. $graphNode = $response->getGraphNode();
  88. return ['status' => true, 'data' => ['id' => $graphNode['id']]];
  89. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  90. $errorMsg = 'Graph API错误:' . $e->getMessage();
  91. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  92. $errorMsg = 'SDK错误:' . $e->getMessage();
  93. }
  94. return ['status'=>false,'data' => $errorMsg];
  95. }
  96. public function getComments($postId) {
  97. }
  98. public function replyToComment($commentId) {
  99. }
  100. public function deleteComment($commentId) {
  101. }
  102. /**
  103. * 获取Facebook用户信息
  104. * @param string $accessToken
  105. * @return array [
  106. * 'name' => string,
  107. * 'id' => string,
  108. * 'error' => string|null
  109. * ]
  110. */
  111. public function getFacebookUser($accessToken)
  112. {
  113. try {
  114. // 验证并设置访问令牌
  115. $this->fb->setDefaultAccessToken($accessToken);
  116. // 发送请求获取用户信息
  117. $response = $this->fb->get('/me?fields=name,id');
  118. $userNode = $response->getGraphUser();
  119. return [
  120. 'name' => $userNode->getName(),
  121. 'id' => $userNode->getId(),
  122. 'error' => null
  123. ];
  124. } catch (FacebookResponseException $e) {
  125. // API 响应错误处理
  126. return [
  127. 'name' => null,
  128. 'id' => null,
  129. 'error' => 'Graph API Error: ' . $e->getMessage()
  130. ];
  131. } catch (FacebookSDKException $e) {
  132. // SDK 错误处理
  133. return [
  134. 'name' => null,
  135. 'id' => null,
  136. 'error' => 'SDK Error: ' . $e->getMessage()
  137. ];
  138. } catch (Exception $e) {
  139. // 其他错误处理
  140. return [
  141. 'name' => null,
  142. 'id' => null,
  143. 'error' => 'General Error: ' . $e->getMessage()
  144. ];
  145. }
  146. }
  147. }