Search code examples
linkedin-api

Linkedin Code Sample returning Null User object


https://developer.linkedin.com/documents/code-samples

Using the above PHP code sample, I can log in to my user profile as expected, (after creating the required "application" and getting the API KEY and SECRET, of course.)

However the $user object returned by the Fetch() function is null, so I do not have access to the profile content via the code, such as the $user->firstname and $user->lastname, etc.

FYI: This question was asked before and not answered: LinkedIn Code Sample not returning ay data


Solution

  • The issue I found was that file_get_contents() was not working, and this was because allow_url_fopen was not enabled in PHP.ini. I believe allow_url_fopen is not enabled by default, as it is a security issue. I found a work-around online using cUrl. See the code below.

    //First, in getAccessToken() replace $response = file_get_contents($url, false, $context) with $response = curl_get_contents($url);

    Than add the following function to the code sample.

    function curl_get_contents($url)
    {
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
    
        $data = curl_exec($ch);
        curl_close($ch);
    
        return $data;
    }