如何打印个人行的配置文件详细信息

时间:2013-09-07 05:47:16

标签: perl linkedin

#!/usr/bin/perl -w

use WWW::LinkedIn;
use CGI;    # load CGI routines
use CGI::Session;
$q = CGI->new;                      # create new CGI object
print $q->header,                   # create the HTTP header
  $q->start_html('hello world'),    # start the HTML
  $q->h1('hello world'),            # level 1 header
  $q->end_html;                     # end the HTML
my $consumer_key    = 'xxxxxxx';
my $consumer_secret = 'xxxxxxxxx';
my $li              = WWW::LinkedIn->new(
    consumer_key    => $consumer_key,
    consumer_secret => $consumer_secret,
);

if ( length( $ENV{'QUERY_STRING'} ) > 0 ) { 
    $buffer = $ENV{'QUERY_STRING'};
    @pairs = split( /&/, $buffer );

    foreach $pair (@pairs) {
        ( $name, $value ) = split( /=/, $pair );
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $in{$name} = $value;
    }   
    $sid = $q->cookie('CGISESSID') || $q->param('CGISESSID') || undef;

    $session = new CGI::Session( undef, $sid, { Directory => '/tmp' } );

    my $access_token = $li->get_access_token(
        verifier             => $in{'oauth_verifier'},
        request_token        => $session->param("request_token"),
        request_token_secret => $session->param("request_token_secret"),
    );  
    undef($session);
    my $profile_xml = $li->request(
        request_url =>
'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,positions,industry,distance)',
        access_token        => $access_token->{token},
        access_token_secret => $access_token->{secret},
    );  
    print $profile_xml;
}

输出以单行打印。我想打印这是单独的一行。

输出

aAVGFD34 jj DD 456456 2003 6 true ara systems Technology and Services  Technology and Services 0 

如何从profile_xml变量获取每个列值?

id avsdff
first name jj
lastname dd

3 个答案:

答案 0 :(得分:0)

只需使用Data :: Dumper和XML :: Simple。

use Data::Dumper; 
use XML::Simple; #you may want to install a specific package from your distribution

{...}
my $hash_ref = SimpeXML::XMLin($profile_xml);
print Dumper($hash_ref);

我不知道你是否想要更精美的输出。

答案 1 :(得分:0)

尝试从哈希引用

中简单打印出来
 foreach $key (keys %{$profile_xml}) {
     print "$key $profile_xml->{$key}\n";
 }

答案 2 :(得分:0)

在这里,我将展示解析数据并在各行中打印的方式。

  my $parser = XML::Parser->new( Style => 'Tree' );
  my $tree   = $parser->parse( $profile_xml );
  #print Dumper( $tree ); you can use this see the data displayed in the tree  formatted

   my $UID = $tree->[1]->[4]->[2],"\n";
   print "User ID:$UID";
   print"</br>";

   my $FirstName = $tree->[1]->[8]->[2],"\n";
   print "First Name:$FirstName";
   print"</br>";

我为UID和FirstName展示的样本。这很好。