2

I am trying to build a Linkedin API code where I am finding a strange error

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

Which I am unable to replicate how to solve the issue.

I have followed the steps for

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

I already downloaded the Vendor by Composer and here is the code I have used

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>';
}
saurabh kamble
  • 1,510
  • 2
  • 25
  • 42
Hritam
  • 51
  • 1
  • 1
  • 5

3 Answers3

4

changes to invoke Dotenv class custom env -->used to run multiple envs

Git link for the package :Git link for the package

 $dotenv = Dotenv\Dotenv::create(dirname(__DIR__), 'custom env');
 $dotenv->load();
  • Was facing this issue when i was migrating from laravel 5.8 to laravel 6

  • note please add help package as the support is remove from core files in laravel 6

saurabh kamble
  • 1,510
  • 2
  • 25
  • 42
3

if you are using 4.1...

Mutable init

$dotenv = Dotenv\Dotenv::createMutable(__DIR__, $evn_file);
$dotenv->load();

Immutable init

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__, $evn_file);
$dotenv->load();
Artistan
  • 1,982
  • 22
  • 33
0

Not sure if this is in the same arena (PHP is not my forte) but I found this one liner over at CraftCMS and it's worked very well for me:

Dotenv\Dotenv::createUnsafeMutable(ENV_PATH)->safeLoad();

tfantina
  • 788
  • 11
  • 37
  • Kind of hoping that if there is an issue with this I will Cunningham's Law my way into a correct answer. – tfantina Feb 27 '23 at 22:48