HereToHelpPHP
HereToHelpPHP

Reputation: 189

Inserting data to mysql from .xml php array

Im trying to get some data from the LinkedIn API using OAuth 2.0. I've got the data insert for name done, but i can't seem to get the skills values. If tried to implode the data but it doesnt find any data. I know the data is recieved.

My code:

    <?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;
    }
?>

The arrays:

  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] => ########
        )

)

Upvotes: 1

Views: 112

Answers (1)

EngineerCoder
EngineerCoder

Reputation: 1455

These are many multiple array try that :

$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"];

Good Luck.

Upvotes: 1

Related Questions