Timsy05 posted on 2011-03-17 02:14:11 #
Hi
I am new to using twitter API.I want to make an application where i want to map all teh tweets based on a search query( say keyword).I am trying to learn how to do it step by step.
First I want to retrieve all the tweets based on a search keyword say "worldcup".
While reading the twitter search API documentation ,I directly ran this query( http://search.twitter.com/search.json?result_type=mixed&q=worldcup) in my browser and got a set of tweets.but I am not sure whether I am using it the right way in the code or not .
Well i tried it ,but it gives an error:
"
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\Search2.php:10 Stack trace: #0 C:\xampp\htdocs\Search2.php(10): SimpleXMLElement->__construct('{"results":[{"f...') #1 {main} thrown in C:\xampp\htdocs\Search2.php on line 10
"
CODE
<?php
$url = "http://search.twitter.com/search.json?result_type=mixed&q=worldcup";
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec( $curl );
curl_close( $curl );
$return = new SimpleXMLElement( $result );
return $return;
?>
The most popular forum posts:
Comments / discussions
Timsy05 posted on 2011-03-17 02:16:17 #
So please can somebody help me with this?
Olaf posted on 2011-03-18 07:28:24 #
Hi Tim,
do you checked this twitter api tutorial?
Timsy05 posted on 2011-03-21 04:51:12 #
Hi Olaf ,
Sorry for the late reply .I was little busy .
But,I did look into the link you have given .I have got something to work.
But I am stuck at a point so please could you help me here.
My code is
[code]
Page1.php
<html>
<div id="search">
<form action="Page2.php" method="get">
<label>
Search twitter
<input type="text" name="q" id="searchbox" />
<input type="submit" name="submit" id="submit" value="Search" />
</label>
</form>
</div>
</html>
Page2.php
<html>
<head></head>
<body>
<?php
include('Page3.php');
if($_GET['q'])
{
$twitter_query = $_GET['q'];
$search = new TwitterSearch();
$results = $search->results($twitter_query);
}
?>
<table border="1">
<?php
foreach($results as $result){
?>
<tr><td>
<?php
echo $result->content;
echo $result->text; ..........Does not work
echo $result->from_user .......Does not work
echo '<br> </br>';
?>
</td></tr>
<?php
}
?>
</table>
</body>
</html>
Page3.php
<?php
Class TwitterSearch
{
function __construct()
{
}
function results($twitter_query)
{
echo $twitter_query;
$url = "http://search.twitter.com/search.atom?q=" . urlencode( $twitter_query ) . "&lang=en&rpp=50";
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec( $curl );
curl_close( $curl );
$return = new SimpleXMLElement( $result );
return $return;
}
}
?>
[/code]
SO,Now I get my results using result->contents but i do not get them from
result->text or text->from_user.Nor the image.
Do you know why is this happening?
Olaf posted on 2011-03-21 07:39:57 #
Hi,
you didn't read the tutorial very well:
foreach( $results->entry as $result ) {
echo "<h3><a>author->uri ."\">". $result->author->name ."<a/></a></h3><img />link[1]->attributes()->href ."\" style=\"float: left;\"><p>". $result->content ."</p><div style=\"clear:both;\"> </div>";
}
You're using the wrong variables for the user's name and text.
Timsy05 posted on 2011-03-24 23:37:22 #
Thank for that.I thought I was using json format.Got it now!
Well I have now tried doing it using json where i changed the following feilds
Page3.php
$url = "http://search.twitter.com/search.json?q=" . urlencode( $twitter_query ) . "&lang=en&rpp=25";
$return=json_decode($result,true);
return $return;
Page2.php
foreach( $results as $result )
{
echo $result->text;
echo $result->from_user;
}
(the rest of the code is same)
Here i do not get any errors but nothing is being outputted.So could you help me here and am I missing anything?
Olaf posted on 2011-03-25 04:49:32 #
Check the php manual :)
if you serialize a JSON string you get an array and not an object.
Timsy05 posted on 2011-03-25 05:57:06 #