Welcome Guest, Not a member yet? Register   Sign In
Twitter Library - How does it work?
#1

[eluser]evolutionxbox[/eluser]
I have tried using Elliot Haughin's Twitter Library, but I don't really understand what is going on.

I understand you need to use the Oauth request codes to get access codes, using them to retrieve the info.

The thing is that his example implementation seems rather bloated unless you need all that code. All I really want to do is get the last 4/5 status updates of a user, then display them (I would preferably like to use a model).

Can anyone help me?
#2

[eluser]Phil Sturgeon[/eluser]
If you don't need all the OAuth stuff, have a play with Simon Maddox's Twitter library.
#3

[eluser]evolutionxbox[/eluser]
I have put the library in the 'application/libraries' folder and have loaded it.
Here is my controller code:
Code:
<?php

class Test extends Controller {

    function Test()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->twitter->auth('username','pa$$word');        
        $data['twitter'] = $this->twitter->user_timeline();
        
        // Build the thing
        $this->load->view('test', $data);
    }
}

/* End of file test.php */
/* Location: ./system/application/controllers/test.php */

All I get is a mass of php errors and warnings... what have I done wrong?
(By the way the view 'test' is a blank html page.)


The errors:
Code:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: Entity: line 61: parser error : Entity 'rsaquo' not defined</p>
<p>Filename: libraries/twitter.php</p>
<p>Line Number: 460</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]:           &lt;li class=&quot;first&quot;&gt;&lt;a href=&quot;http://twitter.com&quot;&gt;Home &amp;rsaquo;&lt;/a&gt;&lt;/li&gt;</p>

<p>Filename: libraries/twitter.php</p>
<p>Line Number: 460</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]:                                                                       ^</p>
<p>Filename: libraries/twitter.php</p>

<p>Line Number: 460</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: Entity: line 82: parser error : Entity 'copy' not defined</p>
<p>Filename: libraries/twitter.php</p>
<p>Line Number: 460</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]:           &lt;li class=&quot;first&quot;&gt;&amp;copy; 2010 Twitter&lt;/li&gt;</p>
<p>Filename: libraries/twitter.php</p>

<p>Line Number: 460</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]:                                   ^</p>
<p>Filename: libraries/twitter.php</p>
<p>Line Number: 460</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined property: stdClass::$request</p>
<p>Filename: libraries/twitter.php</p>
<p>Line Number: 419</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined property: stdClass::$error</p>
<p>Filename: libraries/twitter.php</p>
<p>Line Number: 419</p>

</div>
#4

[eluser]Phil Sturgeon[/eluser]
For now just use JSON:

Code:
$this->twitter->auth('username','pa$$word');
$this->twitter->type = 'json';
$data['twitter'] = $this->twitter->user_timeline();

I will look into this if I get time as I am maintaining this library for him these days.
#5

[eluser]evolutionxbox[/eluser]
How do I get to the info I need?

I use to use a simpleXML object foreach loop:
Code:
foreach($data['twitter']->item as $item) {
    echo "<h1>".$item->heading."</h1>";
    ...
}
#6

[eluser]Phil Sturgeon[/eluser]
Well if you arent using XML then I wouldn't expect simpleXML to work ;-)

var_dump the returned result to see whats happening, then react accordingly.

Like I said, I've never had an issue with the Twitter library and XML but seems like you are getting encoding issues. JSON will avoid character encoding issues such as unescpaed HTML entities so its much easier to work with.
#7

[eluser]evolutionxbox[/eluser]
BTW what are the arguments for the 'user_timeline' function?

Code:
function user_timeline($id = '', $count = '', $since = '', $since_id = '', $page = ''){

What do 'since', 'since_id' and 'page' mean?

...

Using this code:
Code:
$data['twitter'] = $this->twitter->user_timeline('username', 5);

I now get this single error:
Code:
<p>Severity: Warning</p>
<p>Message:  Invalid argument supplied for foreach()</p>
<p>Filename: libraries/twitter.php</p>
<p>Line Number: 91</p>

I think this is due to the fact that the twitter library is converting the xml from simpleXML objects into json stdObject's.
#8

[eluser]evolutionxbox[/eluser]
Please... I kinda have a deadline to stick to.

It's a little silly for someone to release a library that has invalid code.
All I have done to get this is:
- Copy 'twitter.php' (lowercase T) to 'application/library'
- Auto loaded the library
- Used the code above in the 'test' controller

All using the default xml type value.

Anyone?
#9

[eluser]evolutionxbox[/eluser]
I don't mean to be rude but I really need some help.

I could resort to using cURL but I don't know how to incorporate the security functions that Twitter requires.
#10

[eluser]bretticus[/eluser]
Twitter requires simple basic http authentication. It's actually quite easy to send with the cURL library. See this great nettuts tutorial.

Quote:It’s a little silly for someone to release a library that has invalid code.

I think it's far sillier to expect code to work perfectly on every system and configuration. And more so when the code in question is absolutely free. Smile

No one in here actually owes you anything (even so, much has been given.) So, yes, there's no reason to be rude. Smile

Good luck on your deadline.




Theme © iAndrew 2016 - Forum software by © MyBB