使用Linkedin API进行SimpleXML数组格式化

时间:2015-06-07 04:58:44

标签: wordpress simplexml linkedin linkedin-api

我使用以下函数通过公共API导入我的用户linkedin信息。

所有数据都正确导入,除了'位置'这是一个数组。

导入职位的正确方法是什么?

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 个答案:

没有答案