首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dotenv\Dotenv::__construct()不是Dotenv\Loader的实例

Dotenv\Dotenv::__construct()不是Dotenv\Loader的实例
EN

Stack Overflow用户
提问于 2019-08-01 18:43:36
回答 2查看 5.5K关注 0票数 1

我正在尝试构建一个Linkedin API代码,但发现了一个奇怪的错误

代码语言:javascript
复制
Fatal error: Uncaught TypeError: Argument 1 passed to Dotenv\Dotenv::__construct() must be an instance of Dotenv\Loader, string given, called in E:\xampp\htdocs\linkedinpi\examples\index.php on line 16 and defined in E:\xampp\htdocs\linkedinpi\vendor\vlucas\phpdotenv\src\Dotenv.php:31 Stack trace: #0 E:\xampp\htdocs\linkedinpi\examples\index.php(16): Dotenv\Dotenv->__construct('E:\\xampp\\htdocs...') #1 {main} thrown in E:\xampp\htdocs\linkedinpi\vendor\vlucas\phpdotenv\src\Dotenv.php on line 31

我无法复制如何解决这个问题。

我已经按照以下步骤进行了

https://github.com/zoonman/linkedin-api-php-clienthttps://github.com/zoonman/linkedin-api-php-client/tree/master/examples

我已经下载了Vendor by Composer,下面是我使用的代码

代码语言:javascript
复制
include_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';


    use LinkedIn\Client;
    use LinkedIn\Scope;


    $dotenv = new Dotenv\Dotenv(dirname(__DIR__));
    $dotenv->load();

    session_start();

    $client = new Client(
        getenv('[LINKEDIN CLIENT ID]'),
        getenv('[LINKEDIN CLIENT SECRET]')
    );


    if (isset($_GET['code'])) { // we are returning back from LinkedIn with the code
        if (isset($_GET['state']) &&  // and state parameter in place
            isset($_SESSION['state']) && // and we have have stored state
            $_GET['state'] === $_SESSION['state'] // and it is our request
        ) {
            try {
                // you have to set initially used redirect url to be able
                // to retrieve access token
                $client->setRedirectUrl($_SESSION['redirect_url']);
                // retrieve access token using code provided by LinkedIn
                $accessToken = $client->getAccessToken($_GET['code']);
                h1('Access token');
                pp($accessToken); // print the access token content
                h1('Profile');
                // perform api call to get profile information
                $profile = $client->get(
                    'people/~:(id,email-address,first-name,last-name)'
                );
                pp($profile); // print profile information

            $share = $client->post(
                'people/~/shares',
                [
                    'comment' => 'Checkout this amazing PHP SDK for LinkedIn!',
                    'content' => [
                        'title' => 'PHP Client for LinkedIn API',
                        'description' => 'OAuth 2 flow, composer Package',
                        'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client',
                        'submitted-image-url' => 'https://github.com/fluidicon.png',
                    ],
                    'visibility' => [
                        'code' => 'anyone'
                    ]
                ]
            );
            pp($share);


            $companyId = '2414183';

            h1('Company information');
            $companyInfo = $client->get('companies/' . $companyId . ':(id,name,num-followers,description)');
            pp($companyInfo);

            h1('Sharing on company page');
            $companyShare = $client->post(
                'companies/' . $companyId . '/shares',
                [
                    'comment' =>
                        sprintf(
                            '%s %s just tried this amazing PHP SDK for LinkedIn!',
                            $profile['firstName'],
                            $profile['lastName']
                        ),
                    'content' => [
                        'title' => 'PHP Client for LinkedIn API',
                        'description' => 'OAuth 2 flow, composer Package',
                        'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client',
                        'submitted-image-url' => 'https://github.com/fluidicon.png',
                    ],
                    'visibility' => [
                        'code' => 'anyone'
                    ]
                ]
            );
            pp($companyShare);



            $filename = './demo.jpg';
            $client->setApiRoot('https://api.linkedin.com/');
            $mp = $client->upload($filename);
            */
        } catch (\LinkedIn\Exception $exception) {
            // in case of failure, provide with details
            pp($exception);
            pp($_SESSION);
        }
        echo '<a href="/">Start over</a>';
    } else {

        echo 'Invalid state!';
        pp($_GET);
        pp($_SESSION);
        echo '<a href="/">Start over</a>';
    }

} elseif (isset($_GET['error'])) {

    pp($_GET);
    echo '<a href="/">Start over</a>';
} else {
    // define desired list of scopes
    $scopes = [
        Scope::READ_BASIC_PROFILE,
        Scope::READ_EMAIL_ADDRESS,
        Scope::MANAGE_COMPANY,
        Scope::SHARING,
    ];
    $loginUrl = $client->getLoginUrl($scopes); 
    $_SESSION['state'] = $client->getState(); 
    $_SESSION['redirect_url'] = $client->getRedirectUrl(); 
    echo 'LoginUrl: <a href="'.$loginUrl.'">' . $loginUrl. '</a>';
}


function pp($anything)
{
    echo '<pre>' . print_r($anything, true) . '</pre>';
}


function h1($h) {
    echo '<h1>' . $h . '</h1>';
}
EN

回答 2

Stack Overflow用户

发布于 2020-04-04 03:23:58

如果您使用的是4.1...

Mutable init

代码语言:javascript
复制
$dotenv = Dotenv\Dotenv::createMutable(__DIR__, $evn_file);
$dotenv->load();

Immutable init

代码语言:javascript
复制
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__, $evn_file);
$dotenv->load();
票数 3
EN

Stack Overflow用户

发布于 2019-11-06 17:10:26

更改为调用Dotenv类自定义环境-->用于运行多个环境

包的Git链接:Git link for the package

代码语言:javascript
复制
 $dotenv = Dotenv\Dotenv::create(dirname(__DIR__), 'custom env');
 $dotenv->load();

当我从laravel 5.8迁移到laravel 6

  • 时,
  • 遇到了这个问题。注意:请添加帮助包,因为在laravel 6

中删除了对核心文件的支持

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57307238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档