5

I have a controller Callback.php in folder app/code/Dots/Socialfeeds/Controller/Social in custom module Dots_Socialfeeds.

I am also using an third party library file Linkedin.php which I have placed in folder lib/external/Dots/Socialfeeds/Linkedin.

Now I want to use this lib class in my above controller Callback.php.

code--

<?php

namespace Dots\Socialfeeds\Controller\Social;

use Magento\Framework\App\Action\Action;
use Dots\Socialfeeds\Linkedin\Linkedin;

/**
 * Class Callback
 *
 * @package Dots\Socialfeeds\Controller\Social
 */
class Callback extends Action
{
    /**
     * @inheritdoc
     */
    public function execute()
    {       
        $li =   new \Linkedin(
                      array(
                        'api_key' => '81a592y6mz9o33', 
                        'api_secret' => 'kviv5GtPAt3ZDHpq', 
                        'callback_url' => 'http://ds01.projectstatus.co.uk/c2cwp/test.php?type=linkedin'
                      )
                    );
       $token = $li->getAccessToken($_GET['code']);
    }


}

and lib file code --

    <?php
namespace Dots\Socialfeeds\Linkedin;

class Linkedin
{
private $_config = array();
private $_state = null;
private $_access_token = null;
private $_access_token_expires = null;
private $_debug_info = null;
private $_curl_handle = null;

const API_BASE = 'https://api.linkedin.com/v1';
const OAUTH_BASE = 'https://www.linkedin.com/uas/oauth2';

const SCOPE_BASIC_PROFILE = 'r_basicprofile'; // Name, photo, headline, and current positions
const SCOPE_FULL_PROFILE = 'r_fullprofile'; // Full profile including experience, education, skills, and recommendations
const SCOPE_EMAIL_ADDRESS = 'r_emailaddress'; // The primary email address you use for your LinkedIn account
const SCOPE_NETWORK = 'r_network'; // Your 1st and 2nd degree connections
const SCOPE_CONTACT_INFO = 'r_contactinfo'; // Address, phone number, and bound accounts
const SCOPE_READ_WRITE_UPDATES = 'rw_nus'; // Retrieve and post updates to LinkedIn as you
const SCOPE_READ_WRITE_GROUPS = 'rw_groups'; // Retrieve and post group discussions as you
const SCOPE_READ_WRITE_COMPANY_ADMIN = 'rw_company_admin'; // Administers a company as you
const SCOPE_WRITE_MESSAGES = 'w_messages'; // Send messages and invitations to connect as you
const SCOPE_WRITE_SHARE = 'w_share'; // Share url to your contacts

const HTTP_METHOD_GET = 'GET';
const HTTP_METHOD_POST = 'POST';
const HTTP_METHOD_PUT = 'PUT';
const HTTP_METHOD_DELETE = 'DELETE';

/**
 * @param array $config (api_key, api_secret, callback_url)
 * @throws \InvalidArgumentException
 * @throws \RuntimeException
 */
public function __construct(array $config)
{
    if (!isset($config['api_key']) || empty($config['api_key'])) {
        throw new \InvalidArgumentException('Invalid api key - make sure api_key is defined in the config array');
    }

    if (!isset($config['api_secret']) || empty($config['api_secret'])) {
        throw new \InvalidArgumentException('Invalid api secret - make sure api_secret is defined in the config array');
    }

    if (!isset($config['callback_url']) || empty($config['callback_url'])) {
        throw new \InvalidArgumentException('Invalid callback url - make sure callback_url is defined in the config array');
    }

    if (!extension_loaded('curl')) {
        throw new \RuntimeException('PHP CURL extension does not seem to be loaded');
    }

    $this->_config = $config;
}

 .....
 .....

I am getting error if in controller file-

Fatal error: Uncaught Error: Class 'Linkedin' not found in app/code/Dots/Socialfeeds/Controller/Social/Callback.php:24

  • Did you check my comment? – Rohan Hapani Sep 18 '18 at 8:23
1

You have to inject this class Dots/Socialfeeds/Linkedin _construct function of Dots\Socialfeeds\Controller\Social\Callback.

class Callback extends Action
{
  protected $linkedin;     
  public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Dots\Socialfeeds\Linkedin $Linkedin
    ) {
        $this->linkedin = $Linkedin(
                      array(
                        'api_key' => '81a592y6mz9o33', 
                        'api_secret' => 'kviv5GtPAt3ZDHpq', 
                        'callback_url' => 'http://ds01.projectstatus.co.uk/c2cwp/test.php?type=linkedin'
                      )
                    );       
        parent::__construct($context);
    }

    public function execute()
    {       
        $li =   $this->linkedin;
       $token = $li->getAccessToken($_GET['code']);
    }
  • I have tried it and getting this error-- {"0":"Missing required argument $config of Dots\\Socialfeeds\\Linkedin\\Linkedin.","1":"#0 \/home\/c2cmage\/public_html\/vendor\/magento\/framework\/ObjectManager\/Factory\/Dynamic\/Developer.php(34): Magento\\Framework\\ObjectManager\\Factory\\AbstractFactory->resolveArgumentsInRuntime('Dots\\\\Socialfeed...', Array, Array)\n#1 \/home\/c2cmage\/public_html\/vendor\/magento\/framework\/ObjectManager\/Factory\/Dynamic\/Developer.php(59): – Shashank Kumrawat Sep 18 '18 at 9:24
  • I have updated linkedin.php , kindly check above code – Shashank Kumrawat Sep 18 '18 at 9:25
0

In your module put this in registration.php

require_once(BP.'/lib/external/Dots/Socialfeeds/Linkedin.php');
0

You can add this line in your controller :

require_once(BP.'/lib/external/Dots/Socialfeeds/Linkedin.php')
  • I think it should be require_once() not required_once. I have used it still now working. I think use of require _once function in controller is not a standard way to implement. – Shashank Kumrawat Sep 18 '18 at 5:58
  • I have used this but still getting same error – Shashank Kumrawat Sep 18 '18 at 6:04
  • Yes. U r right. It's require. I will change it. – Rohan Hapani Sep 18 '18 at 6:18
  • You can add it by xml file also. For that, You need to create lib/web/Socialfeeds/Linkedin.php folder like this. – Rohan Hapani Sep 18 '18 at 6:19
  • Like : <item type="file">Lib::Socialfeeds/Linkedin.php</item> – Rohan Hapani Sep 18 '18 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.