Welcome Guest, Not a member yet? Register   Sign In
Newbie Q: How to save XML data into Database?
#1

[eluser]Unknown[/eluser]
The Goal:
I would like to save specific pieces of data from Twitter's "show/users" method (specifically, a user's follower_count and friends_count) into a database. The purpose is to track these two pieces of data over time.

What I've Done So Far:
Still learning CI, so I started from scratch and installed CI 1.7.2. I found Phil Sturgeon's very cool REST Client app on Github -- installed that. I created my controller per Phil's example:

Code:
<?php
class Twitter extends Controller {

    function __construct()
    {
        parent::Controller();
        
        $this->load->library('rest', array(
            'server' => 'http://twitter.com/',
            'http_user' => 'username',
            'http_pass' => 'password',
            'http_auth' => 'basic'
        ));
    }

    function show_user($username = 'darkufo')
    {
        if(!$username)
        {
            show_error('Please enter a user in the title. Eg: '.anchor('twitter/show_users/darkufo', 'twitter/show_users/darkufo'));
        }
        
        $result = $this->rest->get('users/show/'.$username);
        foreach( $result as $thing => $value )
        {
            echo '<strong>'.$thing.':</strong> '.$value.'<br/>';
        }
        $this->twitter_model->addInfo($result);
    }
    
    function tweets($username = 'darkufo')
    {
        if(!$username)
        {
            show_error('Please enter a user in the title. Eg: '.anchor('twitter/show_users/darkufo', 'twitter/show_users/darkufo'));
        }

        $this->load->library('rest', array(
            'server' => 'http://twitter.com/'
        ));
        $tweets = $this->rest->get('statuses/user_timeline/'.$username.'.xml');
        echo $this->rest->debug();
    }  
}
?&gt;

I was able to see the results as planned. Now I created my model (this is where I start running into trouble):
Code:
&lt;?php

if (! defined('BASEPATH')) exit('No direct script access');

class Twitter_model extends Model {
    
    function __construct() {
        parent::Model();
    }

    function addInfo($result) {
        print_r($result);

    }
}

I can see that the model is getting hit by viewing the print_r, but I can't seem to figure out how to pull the pieces of the xml result into my database. I plan on using SimpleXML for this, but if there's a better solution, I'm all ears. BTW, all of the necessary libraries (database, curl, and simplexml) and model have been autoloaded.

Any ideas?
#2

[eluser]Christopher Blankenship[/eluser]
ci_newbie:

To help in your model visually in the function addInfo add the following:
Code:
print "<pre>";
print_r($result);
print "</pre>";

From that you will then be able to see the data more structured for reading.

Then to start utilizing SimpleXML see the following: http://us.php.net/manual/en/simplexml.ex...-basic.php

Enjoy!




Theme © iAndrew 2016 - Forum software by © MyBB