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

 
작성일 : 11-06-01 15:43
Twitter Search using the Twitter API and PHP
 글쓴이 : MintState
조회 : 5,412  
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, 2 views)
Rated 0/5 (0%) (0 Votes)

(\__/)
(='.'=)
(")_(")~
Search Infomation by MintState
MintState

 
 

Total 32
번호 제   목 글쓴이 날짜 조회
32 Swiper - Most Modern Mobile Touch Slider MintState 02-02 1517
31 [jquery] 스와프 가능한 이미지 배너 - OWL Carousel MintState 05-30 2843
30 구글맵 api 편리하기 사용가능하게 해주는 gmaps.js MintState 03-04 4327
29 jQuery - FullCalendar MintState 03-21 5920
28 jquery - select box MintState 11-25 5889
27 Wowza Streaming Server MintState 09-19 7660
26 jQuery Datepicker MintState 06-23 6775
25 Using the Twitter Search API MintState 06-01 4736
24 Twitter Search using the Twitter API and PHP MintState 06-01 5413
23 PHP Twitter Search API MintState 06-01 5292
22 모바일(iOS 용) 터치 관련 jQuery 플러그인 MintState 04-18 6338
21 GEO <-> KATEC 변환 MintState 02-21 5518
20 Twitter API 링크 MintState 07-13 7762
19 jQuery Simple WYSIWYG Editor MintState 07-02 8238
18 Prototype.js 의 Object 객체의 메소드 MintState 01-19 6544
17 prototype.js 를 이용한 JSON사용 예제 MintState 01-19 10464
16 php 내장함수을 이용한 XML Parser MintState 08-27 8458
15 XML 파싱(xml2array) MintState 08-27 8100
14 ajax의 xml을 javascript 객체형식으로 자동 변환 MintState 07-07 8370
13 Google Maps API (1) MintState 03-18 14098
 1  2  
모바일 버전으로 보기
CopyRight ©2004 - 2017, YesYo.com MintState. ™