YesYo.com MintState Forums
뒤로    YesYo.com MintState BBS > Tech > Ajax & Issue
검색
멤버이름    오토
비밀번호 
 

Twitter Search using the Twitter API and PHP

페이지 정보

작성자 MintState 댓글 0건 조회 15,359회 작성일 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:

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
첨부 파일
파일 종류: zip twitter.zip (2.3K, 61 views)

댓글목록

등록된 댓글이 없습니다.

Total 32건 1 페이지
Ajax & Issue 목록
번호 제목 글쓴이 조회 날짜
32
Google Maps API 댓글+ 1
MintState 21166 03-18
31 MintState 19320 11-17
30 MintState 19231 10-28
29 MintState 17358 02-11
28 MintState 17229 01-19
27 MintState 16629 09-19
26 MintState 15838 02-16
25 MintState 15677 07-13
24 MintState 15659 08-27
23 MintState 15569 07-07
22 MintState 15497 07-02
열람중 MintState 15360 06-01
20 MintState 15222 10-28
19 MintState 15108 08-27
18 MintState 14722 02-19
17 MintState 14709 10-28
16 MintState 14613 10-28
15 MintState 13263 06-23
14 MintState 13072 04-18
13 MintState 12932 01-05
게시물 검색
모바일 버전으로 보기
CopyRight ©2004 - 2024, YesYo.com MintState. ™