从.xml php array向mysql插入数据

时间:2015-02-22 20:55:21

标签: php mysql

我试图使用OAuth 2.0从LinkedIn API获取一些数据。我已经为名称完成了数据插入,但我似乎无法获得技能值。如果试图破坏数据,但它没有找到任何数据。我知道数据已收到。

我的代码:

    <?php
    session_start();
    require_once('oAuth/config.php');
    require_once('oAuth/linkedinoAuth.php');
    require_once('oAuth/class.linkedClass.php');


    $linkedClass   =   new linkedClass();
    # First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
    $linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret']);
    //$linkedin->debug = true;

   if (isset($_REQUEST['oauth_verifier'])){
        $_SESSION['oauth_verifier']     = $_REQUEST['oauth_verifier'];

        $linkedin->request_token    =   unserialize($_SESSION['requestToken']);
        $linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
        $linkedin->getAccessToken($_REQUEST['oauth_verifier']);
        $_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
   }
   else{
        $linkedin->request_token    =   unserialize($_SESSION['requestToken']);
        $linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
        $linkedin->access_token     =   unserialize($_SESSION['oauth_access_token']);
   }
   $content1 = $linkedClass->linkedinGetUserInfo($_SESSION['requestToken'], $_SESSION['oauth_verifier'], $_SESSION['oauth_access_token']);


    $xml   = simplexml_load_string($content1);
    $array = XML2Array($xml);
    $content = array($xml->getName() => $array);

    /* Info */
    $firstname = $array['first-name'];
    $lastname = $array['last-name'];
    $email = $array['email-address'];
    $headline = $array['headline'];
    $picurl = $array['picture-url'];
    $publicprofileurl = $array['public-profile-url'];

    $summary = $array['summary'];
    $numconnections = $array['num-connections'];

/* SKIILS */
$skills = $array['skills']->skill->skill->name;



    function XML2Array(SimpleXMLElement $parent)
    {
        $array = array();
        foreach ($parent as $name => $element) {
            ($node = & $array[$name])
                && (1 === count($node) ? $node = array($node) : 1)
                && $node = & $node[];
            $node = $element->count() ? XML2Array($element) : trim($element);
        }
        return $array;
    }
?>

数组:

  Array
(
    [person] => Array
        (
            [id] => ########
            [first-name] => ########
            [last-name] => ########
            [languages] => Array

            [skills] => Array
                (
                    [skill] => Array
                        (
                            [id] => 1
                            [skill] => Array
                                (
                                    [name] => Start-ups
                                )

                            [0] => Array
                                (
                                    [id] => 2
                                    [skill] => Array
                                        (
                                            [name] => Business Strategy
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 3
                                    [skill] => Array
                                        (
                                            [name] => Marketing Strategy
                                        )

                                )

                            [2] => Array
                                (
                                    [id] => 12
                                    [skill] => Array
                                        (
                                            [name] => Management
                                        )

                                )



                        )

                )

            [email-address] => ########
            [phone-numbers] => ########
            [im-accounts] => ########
            [twitter-accounts] => ########
            [headline] => ########
            [picture-url] => ########
            [public-profile-url] => ########
        )

)

1 个答案:

答案 0 :(得分:1)

这些是多个阵列尝试:

$person=$array["person"]; // $person is array

//get first array values
$firstname = $person['first-name'];
$lastname = $person['last-name'];
$email = $person['email-address'];
$headline = $person['headline'];
$picurl = $person['picture-url'];
$publicprofileurl = $person['public-profile-url'];

$skills=$person["skills"]; //$skills is array
$skills_inner=$skills["skill"]; //skills_inner is array

//get $skills_inner value (id)
$id=$skills_inner["id"];

$skills_inner_2=$skills_inner["skill"]; // $skills_inner_2 is array

// get $skills_inner_2 value (name)
$name=$skills_inner_2["name"];

祝你好运。