Happyr\LinkedIn\LinkedIn::api PHP Method

api() public method

public api ( $method, $resource, array $options = [] )
$options array
    public function api($method, $resource, array $options = [])
    {
        // Add access token to the headers
        $options['headers']['Authorization'] = sprintf('Bearer %s', (string) $this->getAccessToken());
        // Do logic and adjustments to the options
        $requestFormat = $this->filterRequestOption($options);
        // Generate an url
        $url = $this->getUrlGenerator()->getUrl('api', $resource, isset($options['query']) ? $options['query'] : []);
        $body = isset($options['body']) ? $options['body'] : null;
        $this->lastResponse = $this->getRequestManager()->sendRequest($method, $url, $options['headers'], $body);
        //Get the response data format
        if (isset($options['response_data_type'])) {
            $responseDataType = $options['response_data_type'];
        } else {
            $responseDataType = $this->getResponseDataType();
        }
        return ResponseConverter::convert($this->lastResponse, $requestFormat, $responseDataType);
    }

Usage Example

 public function indexAction()
 {
     $linkedIn = new LinkedIn($this->getServiceLocator()->get('config')['linkedin_keys']['aapId'], $this->getServiceLocator()->get('config')['linkedin_keys']['app_secret']);
     if ($linkedIn->isAuthenticated()) {
         //we know that the user is authenticated now. Start query the API
         $user = $linkedIn->api('v1/people/~:(id,firstName,lastName,emailAddress)');
         //Check user exist or not
         $api = new Api();
         $api_url = $this->getServiceLocator()->get('Config')['api_url']['value'];
         $url = $api_url . "/api/useractivity/";
         $data = array('op' => 'check_exist_user', 'user_name' => $user['emailAddress'], 'email' => $user['emailAddress']);
         $res = $api->curl($url, $data, "POST");
         //Set value in session
         $lk_login = new Container('linkedin');
         $lk_login->first_name = $user['firstName'];
         $lk_login->last_name = $user['lastName'];
         $lk_login->user_name = $user['emailAddress'];
         $lk_login->email = $user['emailAddress'];
         $lk_login->social_id = $user['id'];
         // END :- Set value in session
         //$facebook1->destroySession(); die;
         // For new user
         if ($res->getStatusCode() != 200) {
             $redirectUrl = 'register/index#register-a';
             return $this->redirect()->toUrl($redirectUrl);
         } else {
             $auth = new FrontEndAuth();
             $session = new Container('frontend');
             $content = json_decode($res->getBody(), true);
             $session->status_id = $content['status_id'];
             $session->userid = $content['id'];
             if ($content['status_id'] != 3) {
                 $session->first_name = $content['first_name'];
                 $session->last_name = $content['last_name'];
                 $session->email = $content['email'];
                 $session->user_name = $content['user_name'];
                 $session->user_type_id = $content['user_type_id'];
                 $session->user_data = $content;
                 $session->last_login = $content['last_login_prev'];
                 $auth->wordpress_login($fbuname);
                 //$redirectUrl = ($session->user_type_id == 4)?array('controller' => 'practitioner', 'action' => 'list'):array('controller' => 'practitioner', 'action' => 'dashboard');
                 $redirectUrl = $session->user_type_id == 4 ? 'list' : 'dashboard';
                 $url = "practitioner/" . $redirectUrl;
                 return $this->redirect()->toUrl($url);
             } else {
                 return $this->redirect()->toRoute('login', array('action' => 'index'));
             }
         }
     } elseif ($linkedIn->hasError()) {
         echo "User canceled the login.";
         exit;
     }
     //if not authenticated
     $url = $linkedIn->getLoginUrl();
     return $this->redirect()->toUrl($url);
     die;
 }