Home > Backend Development > PHP Tutorial > Second-hand recycling website developed using PHP integrates social login function

Second-hand recycling website developed using PHP integrates social login function

WBOY
Release: 2023-07-03 06:08:01
Original
1407 people have browsed it

Second-hand recycling website developed using PHP integrates social login function

With the development of the Internet, the demand in the second-hand recycling market has become increasingly strong. In order to facilitate users and improve the convenience and popularity of the website, we can use PHP to develop a second-hand recycling website and integrate the social login function. This article will introduce how to use PHP to develop a simple second-hand recycling website and use the social login function to provide users with a more convenient login experience. We will use integrating Facebook and Google social login as an example.

1) Use PHP to develop a second-hand recycling website

First, we need to set up a PHP environment. After the construction is completed, we can start developing the second-hand recycling website. Here we will use MVC pattern (Model-View-Controller) to build our website. This is a commonly used architectural pattern that helps us manage our code better.

We can create a file named index.php as the entry file of the website. In this file, we can set routing rules to distribute user requests to different controllers.

<?php
// index.php

// 引入自动加载文件
require 'vendor/autoload.php';

// 实例化路由器
$router = new CoreRouter();

// 添加路由规则
$router->add('/', ['controller' => 'Home', 'action' => 'index']);

// 解析路由
$router->dispatch();
Copy after login

Then, we can create a controller file named "Home.php" to handle user requests.

<?php
// Controllers/Home.php

namespace AppControllers;

class Home
{
    public function index()
    {
        // 处理用户请求
    }
}
Copy after login

Next, we need to create a "View.php" file for rendering the user interface. This file will be responsible for displaying the content of our web pages.

<?php
// Views/View.php

namespace AppViews;

class View
{
    public static function render($view, $args = [])
    {
        extract($args, EXTR_SKIP);

        $file = dirname(__DIR__) . "/Views/$view";

        if (is_readable($file)) {
            require $file;
        } else {
            throw new Exception("View not found: $view");
        }
    }
}
Copy after login

2) Integrate social login function

In order to facilitate users to log in to our second-hand recycling website, we can integrate the social login function of Facebook and Google. Users can log in directly using their social media accounts and access the functionality of the website.

First, we need to create a Facebook developer account to obtain the relevant API key, application ID and application key.

<?php
// config.php

return [
    'facebook' => [
        'app_id' => 'YOUR_APP_ID',
        'app_secret' => 'YOUR_APP_SECRET',
        'default_graph_version' => 'v2.10',
    ],
];
Copy after login

Then, we can create a file called "Facebook.php" to encapsulate the Facebook login code.

<?php
// Libraries/Facebook.php

namespace AppLibraries;

use FacebookFacebook;

class FacebookLogin
{
    private $fb;
    private $helper;

    public function __construct()
    {
        $config = require 'config.php';

        $this->fb = new Facebook([
            'app_id' => $config['facebook']['app_id'],
            'app_secret' => $config['facebook']['app_secret'],
            'default_graph_version' => $config['facebook']['default_graph_version'],
        ]);

        $this->helper = $this->fb->getRedirectLoginHelper();
    }

    public function getLoginUrl($redirectUrl)
    {
        return $this->helper->getLoginUrl($redirectUrl, ['email']);
    }

    public function getUser()
    {
        try {
            $accessToken = $this->helper->getAccessToken();
            $response = $this->fb->get('/me?fields=id,name,email', $accessToken);
            $user = $response->getGraphUser();
            return $user;
        } catch (Exception $e) {
            return null;
        }
    }
}
Copy after login

Next, we can use this Facebook login class in our controller.

<?php
// Controllers/Home.php

namespace AppControllers;

use AppLibrariesFacebookLogin;

class Home
{
    public function index()
    {
        $fb = new FacebookLogin();
        $loginUrl = $fb->getLoginUrl('http://example.com/facebook-callback');

        AppViewsView::render('home.php', ['loginUrl' => $loginUrl]);
    }
}
Copy after login

Finally, we can create a view file named "home.php" to display the login button.

<!-- Views/home.php -->

<a href="<?php echo $loginUrl; ?>">使用Facebook登录</a>
Copy after login

Similarly, we can create a file named "Google.php" to encapsulate the Google login code.

<?php
// Libraries/Google.php

namespace AppLibraries;

use GoogleClient;

class GoogleLogin
{
    private $google;

    public function __construct()
    {
        $this->google = new Client(['client_id' => 'YOUR_CLIENT_ID']);
    }

    public function getLoginUrl($redirectUrl)
    {
        $this->google->setRedirectUri($redirectUrl);
        $this->google->addScope('email');

        return $this->google->createAuthUrl();
    }

    public function getUser()
    {
        $code = $_GET['code'];
        $accessToken = $this->google->fetchAccessTokenWithAuthCode($code);
        $this->google->setAccessToken($accessToken);

        $service = new Google_Service_Oauth2($this->google);
        $user = $service->userinfo->get();

        return $user;
    }
}
Copy after login

Use the Google login class in the controller.

<?php
// Controllers/Home.php

namespace AppControllers;

use AppLibrariesFacebookLogin;
use AppLibrariesGoogleLogin;

class Home
{
    public function index()
    {
        $fb = new FacebookLogin();
        $google = new GoogleLogin();

        $facebookLoginUrl = $fb->getLoginUrl('http://example.com/facebook-callback');
        $googleLoginUrl = $google->getLoginUrl('http://example.com/google-callback');

        AppViewsView::render('home.php', ['facebookLoginUrl' => $facebookLoginUrl, 'googleLoginUrl' => $googleLoginUrl]);
    }
}
Copy after login

Show Google login button in view file.

<!-- Views/home.php -->

<a href="<?php echo $facebookLoginUrl; ?>">使用Facebook登录</a>
<a href="<?php echo $googleLoginUrl; ?>">使用Google登录</a>
Copy after login

In this way, we have completed a simple second-hand recycling website and integrated the social login functions of Facebook and Google. Users can now log in using their social media accounts for a more convenient login experience. Through this example, we can see that developing a second-hand recycling website using PHP and integrating social login is a relatively easy process. I hope this article is helpful to you, and I wish you successful development!

The above is the detailed content of Second-hand recycling website developed using PHP integrates social login function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template