FacebookService.php 5.8 KB

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