Facebook SDK v5 与 Laravel 5.0 的集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37385641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:51:06  来源:igfitidea点击:

Facebook SDK v5 integration with Laravel 5.0

facebooklaravellaravel-5facebook-php-sdkfacebook-graph-api-v2.4

提问by Edvard ?kerberg

I'm trying to make a guide on how to integrate Facebook SDK v5 with Laravel 5.0. Have anyone done this before?

我正在尝试制作有关如何将 Facebook SDK v5 与 Laravel 5.0 集成的指南。以前有人这样做过吗?

First of all i added "facebook/php-sdk-v4" : "~5.0" in composer.json see documentatione here: https://developers.facebook.com/docs/php/gettingstarted

首先,我在 composer.json 中添加了“facebook/php-sdk-v4”:“~5.0”,请参阅此处的文档:https: //developers.facebook.com/docs/php/gettingstarted

{
  "require" : {
    "facebook/php-sdk-v4" : "~5.0"
  }
}

Next step composer install in CMD

下一步在 CMD 中安装 Composer

composer install

Next i added a route:

接下来我添加了一条路线:

Route::get('/test', 'Facebookintegration@test');

Next i returned a view:

接下来我返回了一个视图:

public function test() {

    return view('testpage');

}

Next i try to run the info in the view testpage

接下来我尝试在视图 testpage 中运行信息

<?php

require_once URL::asset('vendor/autoload.php');

$fb = new Facebook\Facebook([
'app_id' => 'YOUR_APP_ID',
'app_secret' => 'YOUR_APP_SECRET',
'default_graph_version' => 'v2.5',
]);

?>

Here is where the problems start and this is my current error

这是问题开始的地方,这是我当前的错误

main(): Failed opening required 'http://localhost/fbintegration/vendor/autoload.php' (include_path='.;C:\php\pear')

anyone know how to correctly link yo the files?

有人知道如何正确链接文件吗?

回答by CodeGuru

https://github.com/SammyK/LaravelFacebookSdk

https://github.com/SammyK/LaravelFacebookSdk

Use this package for Laravel instead.

将此包用于 Laravel。

The code below is to post to facebook after retrieving token from my DB

下面的代码是在从我的数据库中检索令牌后发布到 facebook

Route

路线

  Route::post('schedulePost', 'HomeController@ exampelSchedulePost');

Composer.json

Composer.json

 "require": {
            "sammyk/laravel-facebook-sdk": "^3.0"
        }

Provider and aliases , refer to Docs

提供者和别名,参考文档

'providers' => [
    SammyK\LaravelFacebookSdk\LaravelFacebookSdkServiceProvider::class,
    ];

'aliases' => [
    'Facebook' => SammyK\LaravelFacebookSdk\FacebookFacade::class,
    ];



public function exampelSchedulePost(Request $request)
      { 
         $postPermission = 0;
         $profileToken = DB::table('profiles')->where('user_id',Auth::user()->id)->first();
          $fb = App::make('SammyK\LaravelFacebookSdk\LaravelFacebookSdk');
         if($profileToken){
          try {
            $response = $fb->get('/'.$profileToken->uid.'/permissions', $profileToken->access_token);
            $permissions = $response->getGraphEdge();
            foreach ($permissions as $item) {
              if($item['permission'] == 'publish_actions'){
                if($item['status']== 'declined'){
                  $login_link = $fb->getLoginUrl(['email,publish_actions'], 'http://www.URL.com/facebook/callback');
                  return redirect($login_link);
                  //Get Permission again
                }
              }
            }
          } catch(\Facebook\Exceptions\FacebookSDKException $e) {
            dd($e->getMessage());
          }
         }else{
           $login_link = $fb->getLoginUrl(['email,publish_actions'], 'http://www.URL.com/facebook/callback');
           return redirect($login_link);
         }

回答by melson.jao

It looks like the path issue when you want to require file in your view.

当您想在视图中要求文件时,它看起来像是路径问题。

You can try something like this in you view template:

您可以在视图模板中尝试这样的操作:

require_once ($app['path.base'].'/vendor/facebook/graph-sdk/src/Facebook/autoload.php');

require_once ($app['path.base'].'/vendor/facebook/graph-sdk/src/Facebook/autoload.php');

And the I believe you can get Facebook\Facebook class without problem.

我相信你可以毫无问题地获得 Facebook\Facebook 课程。