SimpleXML array formatting with Linkedin API

71 views Asked by At

Im using the following function to import my users linkedin information via the public API.

All the data is importing properly except 'positions' which is an array.

What is the proper way to import the positions?

function getUserProfile()
    {
        try{
            // http://developer.linkedin.com/docs/DOC-1061
            $response = $this->api->profile('~:(id,first-name,last-name,public-profile-url,picture-url,picture-urls::(original),email-address,headline,summary,location,industry,positions)');
        }
        catch( LinkedInException $e ){
            throw new Exception( "User profile request failed! {$this->providerId} returned an error: $e", 6 );
        }

        if( isset( $response['success'] ) && $response['success'] === TRUE ){
            $data = @ new SimpleXMLElement( $response['linkedin'] );

            if ( ! is_object( $data ) ){
                throw new Exception( "User profile request failed! {$this->providerId} returned an invalid data.", 6 );
            }

            $this->user->profile->identifier  = (string) $data->{'id'};
            $this->user->profile->firstName   = (string) $data->{'first-name'};
            $this->user->profile->lastName    = (string) $data->{'last-name'};
            $this->user->profile->displayName = trim( $this->user->profile->firstName . " " . $this->user->profile->lastName );
            $this->user->profile->industry    = (string) $data->{'industry'};
            $this->user->profile->location    = (string) $data->location->name;
            $this->user->profile->summary    = (string) $data->{'summary'};

            $this->user->profile->positionTitle    = (array) $data->position->title;

            $this->user->profile->email         = (string) $data->{'email-address'};
            $this->user->profile->emailVerified = (string) $data->{'email-address'};

            $this->user->profile->photoURL    = (string) $data->{'picture-url'};
            $this->user->profile->profileURL  = (string) $data->{'public-profile-url'};
            $this->user->profile->description = (string) $data->{'headline'};

            if( $data->{'phone-numbers'} && $data->{'phone-numbers'}->{'phone-number'} ){
                $this->user->profile->phone = (string) $data->{'phone-numbers'}->{'phone-number'}->{'phone-number'};
            }
            else{
                $this->user->profile->phone = null;
            }

            if( $data->{'date-of-birth'} ){
                $this->user->profile->birthDay   = (string) $data->{'date-of-birth'}->day;
                $this->user->profile->birthMonth = (string) $data->{'date-of-birth'}->month;
                $this->user->profile->birthYear  = (string) $data->{'date-of-birth'}->year;
            }

            if( (string) $data->{'picture-urls'}->{'picture-url'} ){ 
                $this->user->profile->photoURL  = (string) $data->{'picture-urls'}->{'picture-url'};
            }

            return $this->user->profile;
        }
        else{
            throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 );
        }
    }
0

There are 0 answers