Twitter Search using the Twitter API and PHP
페이지 정보
작성자 MintState 댓글 0건 조회 15,711회 작성일 11-06-01 15:43본문
Twitter Search using the Twitter API and PHP
In this tutorial we will show you ho-to use the Twitter API for some simple search requests. For all requests we send to the Twitter API we will be using cURL with PHP. You need to check if you have cURL enabled on your server. To test this create a file which contains:
Search the page for cURL section. If you do not have cURL enabled on your server then you will either have to contact technical assistance. If you're using a local server, a quick Google search will tell you tell you how-to enable cURL.
Using Twitter's search api, we don't need to authenticate on the API server what means we don't need to register for an API key and/or secret.
The Twitter PHP Class
To make our script easy to use we're going to build a class to return search results from twitter.
So first of all we are just going to declare our class and define the return type.
Next we are going to build our actual search results function. For this function we will have one parameter which contains the search query.
This function is just using some basic cURL functions and then creating a Simple XML Element and returning it. If you look for the Twitter Search API Method" you will see that the two options for the response are JSON and Atom/XML. For the example above we used Atom/XML. Notice, when making requests you should always urlencode() the q parameter. We also have two other parameters in our url, we have lang and rrp. I'm sure I don't need to explain what lang is (for ISO language codes check this Wiki page). rrp is the number of tweets to Return Per Page, the maximum is 100. This function returns an object so we will have to keep this in mind when using this function later on.
The next function we're going to write will request the weekly trends from twitter and return them in an array.
For the trends we cannot request the data in Atom format, Twitter will return it as JSON so we will use PHP to decode the return.
The function is very similar to the first except this time instead of creating a simple xml element we use json_decode (The second parameter for this function is telling php to convert objects into array elements).
Trends have two parameters dates and exclude which you can read about at this Twitter API page
The finished Twitter class
Using the Twitter class
Save the class as something like twitter.class.php and then create a new blank document. The first thing we want to do in this document is include the class and then create a new instance of the class.
Next we can use a foreach loop to output the data.
Remember that searchResults does not return an array.
Our weekly trends function on the other hand does return an array.
The trends function returns a 3 dimensional array making it slightly harder to use but we still get the results that we want.
You can download the source code used in this tutorial.
URL : http://www.finalwebsites.com/forums/topic/twitter-search-using-the-twitter-api-and-php
In this tutorial we will show you ho-to use the Twitter API for some simple search requests. For all requests we send to the Twitter API we will be using cURL with PHP. You need to check if you have cURL enabled on your server. To test this create a file which contains:
echo phpinfo();
Search the page for cURL section. If you do not have cURL enabled on your server then you will either have to contact technical assistance. If you're using a local server, a quick Google search will tell you tell you how-to enable cURL.
Using Twitter's search api, we don't need to authenticate on the API server what means we don't need to register for an API key and/or secret.
The Twitter PHP Class
To make our script easy to use we're going to build a class to return search results from twitter.
So first of all we are just going to declare our class and define the return type.
class Twitter { public function __construct(){ } }
Next we are going to build our actual search results function. For this function we will have one parameter which contains the search query.
public function searchResults( $search = null ) { $url = "http://search.twitter.com/search.atom?q=" . urlencode( $search ) . "&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; }
This function is just using some basic cURL functions and then creating a Simple XML Element and returning it. If you look for the Twitter Search API Method" you will see that the two options for the response are JSON and Atom/XML. For the example above we used Atom/XML. Notice, when making requests you should always urlencode() the q parameter. We also have two other parameters in our url, we have lang and rrp. I'm sure I don't need to explain what lang is (for ISO language codes check this Wiki page). rrp is the number of tweets to Return Per Page, the maximum is 100. This function returns an object so we will have to keep this in mind when using this function later on.
The next function we're going to write will request the weekly trends from twitter and return them in an array.
For the trends we cannot request the data in Atom format, Twitter will return it as JSON so we will use PHP to decode the return.
public function weeklyTrends() { $url = "http://search.twitter.com/trends/weekly.json"; $curl = curl_init(); curl_setopt( $curl, CURLOPT_URL, $url ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); $result = curl_exec( $curl ); curl_close( $curl ); $return = json_decode( $result, true ); return $return; }
The function is very similar to the first except this time instead of creating a simple xml element we use json_decode (The second parameter for this function is telling php to convert objects into array elements).
Trends have two parameters dates and exclude which you can read about at this Twitter API page
The finished Twitter class
class Twitter { public function __construct(){ } public function searchResults( $search = null ) { $url = "http://search.twitter.com/search.atom?q=" . urlencode( $search ) . "&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; $return = new SimpleXMLElement( $result ); } public function weeklyTrends() { $url = "http://search.twitter.com/trends/weekly.json"; $curl = curl_init(); curl_setopt( $curl, CURLOPT_URL, $url ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); $result = curl_exec( $curl ); curl_close( $curl ); $return = json_decode( $result, true ); return $return; } }
Using the Twitter class
Save the class as something like twitter.class.php and then create a new blank document. The first thing we want to do in this document is include the class and then create a new instance of the class.
require_once("twitter.class.php"); $Twitter = new Twitter;
Next we can use a foreach loop to output the data.
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>"; }
Remember that searchResults does not return an array.
Our weekly trends function on the other hand does return an array.
require_once("twitter.class.php"); $Twitter = new Twitter; $trends = $Twitter->weeklyTrends(); foreach( $trends["trends"] as $date => $trends ) { echo "<h3>". $date ." trends</h3>"; foreach( $trends as $k => $trend ) { echo $trend["query"] .""; } }
The trends function returns a 3 dimensional array making it slightly harder to use but we still get the results that we want.
You can download the source code used in this tutorial.
URL : http://www.finalwebsites.com/forums/topic/twitter-search-using-the-twitter-api-and-php
|
댓글목록
등록된 댓글이 없습니다.