|
本站推荐 |
|
 |
|
热门文章 |
|
 | |
| 用Flash
进行Google |
|
最新更新时间:2005-9-27
22:52:19 浏览次数:
|
|
演示:http://www.productbeta.com/google/index.php
当 SOAP的来临和 XML等相似的到来一样 , 站点开发者可以非常容易的从不同的来源提供其他站的更多信息。
为了增加这种方便,许多不同的Web服务提供了API,诸如Google, Amazon, ebay 以及一些你所知道的名字。
Google作为我最喜爱的搜索引擎(以及数百万个互联网网用户的),
Flash是我的生计工具,这个想法就是建立一个环境可以使Flash能使用Google并且在该Flash中输出结果。
基本需求:
我们需要以下东西建立这个产品
Flash MX PHP 并有XML支持
Nusoap(PHP小组一种,允许开发者建立使用 SOAP网页服务) A Google API
账户(可以在这里获得)
我在这里将不打算介绍SOAP的细节以及如何使用它。在线帮助已经有足够的数据了。无论如何,对XML和它的各种各样的同胞的熟悉是我从读者那里期望的全部。无论如何,我期望读者们能够阅读它。
不过,通过Nusoap 访问Google之后我将大力推荐在Devshed上的这篇文章。
制作步骤
第一步,建立一个PHP文件连接到Google,并在Flash环境中获得搜索结果。下面是该文件的原代码。
这个过程很容易使用Nusoap实现。 <?php //Initialize PHP
//include the nusoap
class..(http://dietrich.ganx4.com/nusoap/)
include_once($document.ROOT."/../scripts/nusoap/nusoap.php");
/*prints the results of the search in a swf file
as an array structure to hold ready made data for
flash*/ function google2Flash($searchStr,
$startParam=0){ if(empty($searchStr)) {
expirationHeaders(); return; }//end if
// create an instance of the SOAP client object
// remember that this script is the client, //
accessing the web service provided by Google $soapclient
= new soapclient("http://api.google.com/search/beta2";);
setType($startParam, "integer"); $maxResults =
10; //Google Limit :( // set up an array
containing input parameters to be // passed to the
remote procedure $params = array( "key" =>
"xxxxxxxxxxxxxxx", // Google license key "q" =>
$searchStr, // search term..given by flash user "start"
=> $startParam*$maxResults, // start from result n
"maxResults" => $maxResults, // show a total of n results..
Google Limit 10 "filter" => false, // remove similar
results "restrict" => "", // restrict by topic
"safeSearch" => false, // adult links? "lr" => "",
// restrict by language "ie" => "", // input
encoding "oe" => "" // output encoding );
// invoke the method on the server $result =
$soapclient->call("doGoogleSearch", $params,
"urn:GoogleSearch", "urn:GoogleSearch"); //Now
result will be an array which contains various result
elements //it is these elements that we are concerned
with $tElements =
sizeof($result["resultElements"]);
if(empty($tElements)) { expirationHeaders();
return; }//end if //Send expiration
headers expirationHeaders(); //Okay so we got
something.. now we have to make a display string for
Flash print "&displayData="; for($i=0;
$i<$tElements; $i++){ //If we got a title the this
result.. if
(!empty($result["resultElements"][$i]["title"])){ //If
no link available if
(empty($result["resultElements"][$i]["URL"])){ print
"<font size=\"12px\"><b>".(($i+1) +
($startParam*$maxResults)).".
".urlencode($result["resultElements"][$i]["title"])."</b></font>";
} else { print "<font
size=\"12px\"><b>".(($i+1) +
($startParam*$maxResults)).". </b><a
href=\"".urlencode($result["resultElements"][$i]["URL"])."\"
target=\"_blank\"><b><u>".urlencode($result["resultElements"][$i]["title"])."</u></b></a></font>";
}//End if print "<br><br>";
//Did we get a summary?
if(!empty($result["resultElements"][$i]["summary"])){
print "<font size=\"10px\"><b>Summary</b>:
".urlencode(ereg_replace("\n|<br>", "",
$result["resultElements"][$i]["summary"]))."</font><br>";
}//End if //Did we get a snippet?
if(!empty($result["resultElements"][$i]["snippet"])){
print "<font
size=\"10px\">".urlencode(ereg_replace("\n|<br>", "",
$result["resultElements"][$i]["snippet"]))."</font><br>";
}//End if print
"-----------------------------------------------------------------------------";
//if we are not on the last element.. print a
break if($i+1 < $tElements){ print
"<br><br>"; }//End if }//End if
}//End for loop //Print out the total results
number that came in print
"&estimatedTotalResultsCount=".$result["estimatedTotalResultsCount"]."&";
//Calculate the total number of pages which will be needed to
show all the results print
"&tPages=".ceil($result["estimatedTotalResultsCount"]/$maxResults)."&";
print
"&searchTime=".urlencode($result["searchTime"])."&";
print
"&searchQuery=".urlencode($result["searchQuery"])."&";
}//End function //Prints an expiration headers
for form-encoded data function
expirationHeaders(){ header("application/x-www-urlform
encoded"); header("Last-Modified: ".gmdate("D, d M Y
H:i:s")." GMT"); //Creation time header("Expires:
".gmdate("D, d M Y H:i:s")." GMT"); //Expiration time.. right
away! header("Cache-Control: no-cache,
must-revalidate"); header("Pragma: no-cache");
}//End function
google2Flash(urldecode($usrInput), $startParam);
//Goodbye PHP ?>
这里是PHP代码的一些难点:
包含Nusoap
传送用户输入和开始的数据(在脚本中还有数据是Flash本身工作的传输)定义google2Flash的PHP功能 。
google2Flash将做如下工作: 连接到Google (使用 nusoap)
在Google使用Flash传送数据执行一个搜索...需要使用Google API组件执行GoogleSearch
传送头部数据确认数据正确到达。
输出Google结果列表数据或其他数据量,发回到Flash并准备显示。Google每一次提交将仅仅提供最大十个数据量,这是Google
API极限,不是我的错。Flash需要输出至少50个吧这样你会觉得更需要些。
在选择可能在Flash返回“准备成功的”显示数据和Flash传递数据组列并分析两者之间,我选择了前者,因为我并不满意
actionscript处理一大堆数据 的速度。在一些PHP工作会比
actionscript快点,它还可以重定义数据显示的样子并不需要产生任何文件,在Flash里执行就可以了,这样方便不少。
进行
Flash制作!
我将设计部分留给了你,
我自己制作的布局并不复杂只是想完成Flash引用Google搜索的过程。本质上,这些Flash的应用是使用loadVars()
组件。
这里是在Flash(通过PHP)从Google获得数据的核心部分。工程的整个代码是比较复杂将需要更多页码的教程在OOP
设计上,我们将跳过这个。
/*############################ GOOGLE
SEARCH ENGINE #############################*/ Google
= new Object(); Google.submitSearch =
function(){ //Create loadvars object this.googleData =
new LoadVars(); this.googleData.usrInput =
this.searchBox.inputFieldMc.usrInput.text; this.googleData.startParam
= this.startParam;
this.googleData.onLoad =
function(){ //We did get some
results if(typeof(this.displayData) !=
"undefined"){ _level0.Google.resultsBox.resultArea.results.htmlText
= this.displayData; delete(this.displayData); //Delete this
really long string! }//End if
this.googled = true;
//Indicator that we are done with onLoad method }//end
function this.googleData.sendAndLoad("/google/scripts/getGoogle.php",
this.googleData, "POST"); }//End
function |
| (BY product beta)
|
|