Home arrow Blog
Main Menu
Home
Blog
Downloads
Forum
Link
Search
Category
Mobile
PHP
Google
Joomla
Cygwin
I-Apply
Linux
Plagger
Internet
Other
What's New
Recommend
RSS
E-Pagerank
Airで作るアプリケーション!PHPと通信してみる。
Writte by Administrator   
2008/03/15 土曜日 01:12
Tag it:
Hatena
Delicious
Spurl
blogmarks
ということで、Airで何かアプリケーションを作ってみようと言うことで、
取りあえず作ってみた。

といっても、せっかくなので外部と通信をしなくてはつまらない。
そんなわけで、まずは実験的な意味を含めて自作のPHPとAirで作った
プログラムを通信させようというのが今回の目標。


まずは、PHPの方については・・・。
まずは、簡単なサンプルを作成するということで、引数2個を受け取り、
その合計値を返すというプログラムを作ることにした。

もちろん、返却はXMLで行う。

と、ソースはこんな感じ。
特に難しいこともなく、ぐっちゃりとハードコーディング・・・。
1年ぶりのPHPでよくわからなかった。まぁいいか。

<?php

  $arg1 = $_REQUEST['arg1'];
  $arg2 = $_REQUEST['arg2'];

  if(is_null($arg1) || is_null($arg2) || !is_numeric($arg1) || !is_numeric($arg2)) {
    $error = true;
  } else {
    $error = false;
    $value = $arg1 + $arg2;
  }
  header("Content-Type: text/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Header>
    <Args>
      <?php if(!is_null($arg1) && is_numeric($arg1)) { ?>
        <Arg name="arg1"><?php print($arg1); ?></Arg>
      <?php } ?>
      <?php if(!is_null($arg2) && is_numeric($arg2)) { ?>
        <Arg name="arg2"><?php print($arg2); ?></Arg>
      <?php } ?>
    </Args>
    <?php if(!$error) { ?>
      <Result><?php print($value); ?></Result>
    <?php } ?>
    <Status><?php if($error){ print("error"); } else { print("ok"); } ?></Status>
  </Header>
</Response>

 プログラムとしてはとっても簡単だ。
arg1とarg2をGETから受け取り、計算を行いあとは、XMLで返すという
だけの簡単なプログラム。

試しに、サーバーに設置を行い確認してみる。
http://hoge.com/api/plus.php?arg1=10&arg2=2

とこのような感じだ

 

 次は、Airのほうだ。
まずはソースを・・・ 

 <?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  <mx:Button x="130" y="13" label="=" id="exeButton" width="34" click="plainRPC.send();" />
  <mx:Text x="172" y="15" width="54" id="ansText"/>
  <mx:TextInput x="10" y="13" width="45" id="arg1Text"/>
  <mx:TextInput x="77" y="13" width="45" id="arg2Text"/>
  <mx:Text x="59" y="17" text="+"/>
  <mx:Script>
    <![CDATA[
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.controls.Alert;

    public function handlePlain(event:ResultEvent):void
    {
      ansText.text = event.result.Header.Result;
    }

    public function handleFault(event:FaultEvent):void
    {
      Alert.show(event.fault.faultString, "Error");
    }
    ]]>
  </mx:Script>

  <mx:HTTPService result="handlePlain(event);" fault="handleFault(event);" id="plainRPC" resultFormat="e4x"
    url="http://labs.zsrv.net/api/plus.php"
    useProxy="false">
    <mx:request xmlns="">
      <arg1>{arg1Text.text}</arg1>
      <arg2>{arg2Text.text}</arg2>
    </mx:request>
  </mx:HTTPService>  
</mx:WindowedApplication>

 とこのような感じとなった。
注意したい点をとりあえず抜粋してみる。

まずは、後半部分のHTTPリクエストを投げる部分について。

<mx:HTTPService result="handlePlain(event);" fault="handleFault(event);" id="plainRPC" resultFormat="e4x"
  url="http://hoge.com/api/plus.php"
  useProxy="false">
  <mx:request xmlns="">
   <arg1>{arg1Text.text}</arg1>
   <arg2>{arg2Text.text}</arg2>
  </mx:request>
 </mx:HTTPService> 

 

ここだ。
まずは、<mx:HTTPService>の要素についてだ。

 

result="handlePlain(event);" 

これについては、リクエストを投げた後にレスポンスが返ってきた場合に
呼ばれる。つまりはコールバック関数だ。
ソースの上部分に、handlePlain(event)というメソッドが定義されているが、
このメソッドが呼び出される。

また、リクエストについては非同期で処理される。

fault="handleFault(event);" 

については、リクエストを投げる処理が失敗したときに呼び出される。
これもまたコールバック関数だ。

id="plainRPC" 

これは、このリクエストに対するIDだ。
実際にボタンイベントが発生したときにこのIDを使いHTTPリクエストを
起動している。

<mx:Button x="130" y="13" label="=" id="exeButton" width="34" click="plainRPC.send();" /> 

 

resultFormat="e4x" 

これは、リクエスト結果をどのようなフォーマットで受け取るかの定義だ。
e4xというのに設定しておくと、返ってきた結果のXMLを「.」でつなぐことで、
値を取得出来るのでとっても楽だ。
(値の取得方法については後述)

urlやproxyについては解説なしで想像つくとおもうので省略。

 

<mx:request xmlns="">
   <arg1>{arg1Text.text}</arg1>
   <arg2>{arg2Text.text}</arg2>
 </mx:request> 

これについては、つまりはリクエストを投げる際の引数となる。
つまり、arg1Textとarg2Text(二つともテキストボックス)に設定された
値を引数とし、この引数をURLとくっつけリクエスト投げることになる。


少し上の方に戻る。次は、
 

public function handlePlain(event:ResultEvent):void
  {
   ansText.text = event.result.Header.Result;
  } 

についてだ。
ここが、リクエストの処理が返ってきたときに呼ばれるコールバック関数
の実態となる。
ここで、結果を受けている。

どのように取得しているかというと引数のeventから、

event.result.Header.Result; 

という形で受け取っている。
ここで、わかりやすいように先ほど作ったPHPの結果のXMLを下記に書いておく。

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Header>
    <Args>
      <Arg name="arg1">10</Arg>
      <Arg name="arg2">5</Arg>
    </Args>
    <Result>15</Result>
    <Status>ok</Status>
  </Header>
</Response> 

となっている。
つまり、結果としては<Result>15</Result>を取得したいわけだ。
XPathで表記すると以下のようになると思う。

/Response/Header/Result 

これを、Airでは、先ほど書いたとおり<Response>をのぞいた、

event.result.Header.Result; 

で取得出来ることになる。「event.result」までがAirとしての構文。
「Header.Result」が、XMLの取得先となる。


以上で無事に、AirからPHPのアクセスを行い足し算結果を取得する
事が出来た。

 

Tag it:
Hatena
Delicious
Spurl
blogmarks

Add as favourites (57) | Quote this article on your site | Views: 2152

  Comments (10)
 1 Comment 01 430
Written by , on 01-11-2008 12:30 , IP: 91.121.120.173
http://klsas.warszawa.pl your site is so great!
 2 Comment 01 826
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 01-11-2008 16:26 , IP: 91.121.120.173
http://klzzsas.warszawa.pl your site is so great!
 3 Comment 01 826
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 01-11-2008 16:26 , IP: 91.121.120.173
http://klzzsas.warszawa.pl your site is so great!
 4 Comment 02 1726
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 03-11-2008 01:26 , IP: 91.121.211.187
http://klsas.warszawa.pl your site is so great!
 5 Comment 02 1844
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 03-11-2008 02:44 , IP: 91.121.120.173
http://klsas.warszawa.pl your site is so great!
 6 Comment 02 2118
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 03-11-2008 05:18 , IP: 91.121.120.173
http://klsas.warszawa.pl your site is so great!
 7 Comment 02 2119
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 03-11-2008 05:19 , IP: 91.121.120.173
http://klsas.warszawa.pl your site is so great!
 8 Comment 05 1946
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 06-11-2008 03:45 , IP: 91.121.120.173
[URL=http://groups.google.dj/group/vxqha2q44dbl/web/homemade-electric-sex-torture-toys]Homemade Electric Sex Torture Toys 602503[/URL] [URL=http://groups.google.tt/group/idiptie2ek/web/short-hot-lesbian-action-movie]Short Hot Lesbian Action Movie 545[/URL] [URL=http://groups.google.la/group/83ujqt1h/web/biggest-cock-in-tn-world]Biggest Cock In Tn World 04262fe3[/URL] [URL=http://groups.google.com.sa/group/jrixjwbro/web/nude-muscle-women]Nude Muscle Women f7126d7[/URL] [URL=http://groups.google.fr/group/lvp3vdlxnoihe/web/celebrity-sex-scenes-free-clips]Celebrity Sex Scenes Free Clips a8d0f95[/URL] [URL=http://groups.google.im/group/4zp3la0auav7li/web/nude-pictures-of-the-girls-next-door]Nude Pictures Of The Girls Next Door 9c70e81f6[/URL] [URL=http://groups.google.com.ai/group/p0jbcqv1o/web/pictures-of-gay-teen-boys-having-sex]Pictures Of Gay Teen Boys Having Sex c37400[/URL] [URL=http://groups.google.com.sa/group/0yohkrok9l9r/web/video-porno-pokemon-gratis]Video Porno Pokemon Gratis 06cc[/URL] [URL=http://groups.google.kg/group/uii7ddztut/web/ebony-bblack-sex]Ebony Bblack Sex 6e4b8e[/URL] [URL=http://groups.google.la/group/83ujqt1h/web/cum-on-her-pussy]Cum On Her Pussy 40a0[/URL] [URL=http://groups.google.tt/group/c9izgae8dwcs/web/baseball-rumors-for-2008]Baseball Rumors For 2008 714b[/URL] [URL=http://groups.google.lv/group/tmqkgiqofsddvuk/web/free-asian-school-girl-porn]Free Asian School Girl Porn f68a7[/URL] [URL=http://groups.google.lv/group/it1zuopv07bapq/web/free-french-cum-drinking-goo-clips]Free French Cum Drinking Goo Clips 7b7b[/URL] [URL=http://groups.google.am/group/olmaa3n2azim/web/free-videos-women-sucking-cock]Free Videos Women Sucking Cock 1a740952[/URL] [URL=http://groups.google.co.za/group/hcvisoo0iyt/web/mexican-big-thong-wearing-ass-porn]Mexican Big Thong Wearing Ass Porn dd5[/URL] [URL=http://groups.google.kg/group/uii7ddztut/web/sexy-teen-lesbian-sex]Sexy Teen Lesbian Sex 56797bde[/URL] [URL=http://groups.google.fr/group/lvp3vdlxnoihe/web/free-sex-cams-live]Free Sex Cams Live 86ca4[/URL] [URL=http://groups.google.com.ng/group/tp8ad2mz4oap/web/hot-moms-having-sex-with-their-son]Hot Moms Having Sex With Their Son 3fe70[/URL] [URL=http://groups.google.com.co/group/u575zjdogxs73am/web/young-girls-fuck-up-ass]Young Girls Fuck Up Ass a1efac72[/URL] [URL=http://groups.google.co.mz/group/ig253yhecav2lil/web/jessica-alba-having-sex-in-as]Jessica Alba Having Sex In As b18f4b[/URL] [URL=http://groups.google.hu/group/fconeatlsu/web/cok-ack-porno-resimler]Cok Ack Porno Resimler 6362e6f[/URL] [URL=http://groups.google.la/group/83ujqt1h/web/women-oral-sex-video]Women Oral Sex Video a18ad2b[/URL] [URL=http://groups.google.tt/group/c9izgae8dwcs/web/blonde-lesbian-babes-kissing]Blonde Lesbian Babes Kissing 52ba6551[/URL] [URL=http://groups.google.ca/group/yq3sbnn5/web/how-so-you-have-oral-sex]How So You Have Oral Sex 8edca230[/URL] [URL=http://groups.google.kg/group/uii7ddztut/web/young-firm-tits-and-ass-pics]Young Firm Tits And Ass Pics 849366f[/URL] [URL=http://groups.google.gp/group/eobfxfkclmj0/web/big-boobs-blonde-bikini]Big Boobs Blonde Bikini 6a69f217[/URL]
 9 Comment 06 043
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 06-11-2008 08:43 , IP: 91.121.120.173
[URL=http://621.chinchillidae.az.pl]cheap flights thompson 15th 9.20pm e66354ea1[/URL] [URL=http://29.synchronize.az.pl]phuket cheap flights d07b9d[/URL] [URL=http://749.galician.az.pl]bodog no deposit free bonus slots codes flash casino 6066[/URL] [URL=http://661.synchronize.az.pl]thomas cook - charter flights 26c764423[/URL] [URL=http://502.galician.az.pl]free online slots 0f20[/URL] [URL=http://299.chinchillidae.az.pl]cheap flights alicante to barcelona 95382f[/URL] [URL=http://695.chinchillidae.az.pl]paris to brisbane cheap flights df44e4[/URL] [URL=http://178.chinchillidae.az.pl]cheap flights and hotels to the philippines bc31[/URL] [URL=http://50.synchronize.az.pl]flights from gatwick to alicante 8a00[/URL] [URL=http://358.synchronize.az.pl]onur air flights to bodrum 9f4e17a98[/URL] [URL=http://220.galician.az.pl]dasino slots free downloads 6704e[/URL] [URL=http://453.synchronize.az.pl]turkey flights virgin 038a44a4b[/URL] [URL=http://787.galician.az.pl]charles town races and slots in charles town, west virginia 9e44[/URL] [URL=http://565.galician.az.pl]wheel of fortune slots for computer 64e[/URL] [URL=http://493.synchronize.az.pl]cheap business flights dublin sydney fdebd03[/URL] [URL=http://776.chinchillidae.az.pl]cheap flights from london to malaga 4c73916[/URL] [URL=http://32.chinchillidae.az.pl]cheap flights to dalaman 22978a4[/URL] [URL=http://657.chinchillidae.az.pl]cheap american domestic flights b27b6ee3[/URL] [URL=http://454.chinchillidae.az.pl]shaheen international flights schedule f7c3a5[/URL] [URL=http://121.chinchillidae.az.pl]cheap flights england to germany c125484[/URL] [URL=http://778.chinchillidae.az.pl]british airways flights to kolkata 141231[/URL] [URL=http://754.chinchillidae.az.pl]cheap flights to spain july 2007 188bc032[/URL] [URL=http://460.galician.az.pl]"charlestown races and slots, wv" 7b1b74e9e[/URL] [URL=http://156.chinchillidae.az.pl]larnaca to uk flights 11f1caa7d[/URL] [URL=http://698.galician.az.pl]liner slots online 5f1fbda4[/URL] [URL=http://336.galician.az.pl]free multi line bonus slots casino style 5b504df2[/URL] [URL=http://269.synchronize.az.pl]delta airlines baggages policy for international flights c9e4be0[/URL]
 10 Comment 06 044
Written by このメールアドレスはスパムボットから保護されています。観覧するにはJavaScriptを有効にして下さい , on 06-11-2008 08:44 , IP: 91.121.120.173
[URL=http://621.chinchillidae.az.pl]cheap flights thompson 15th 9.20pm e66354ea1[/URL] [URL=http://29.synchronize.az.pl]phuket cheap flights d07b9d[/URL] [URL=http://749.galician.az.pl]bodog no deposit free bonus slots codes flash casino 6066[/URL] [URL=http://661.synchronize.az.pl]thomas cook - charter flights 26c764423[/URL] [URL=http://502.galician.az.pl]free online slots 0f20[/URL] [URL=http://299.chinchillidae.az.pl]cheap flights alicante to barcelona 95382f[/URL] [URL=http://695.chinchillidae.az.pl]paris to brisbane cheap flights df44e4[/URL] [URL=http://178.chinchillidae.az.pl]cheap flights and hotels to the philippines bc31[/URL] [URL=http://50.synchronize.az.pl]flights from gatwick to alicante 8a00[/URL] [URL=http://358.synchronize.az.pl]onur air flights to bodrum 9f4e17a98[/URL] [URL=http://220.galician.az.pl]dasino slots free downloads 6704e[/URL] [URL=http://453.synchronize.az.pl]turkey flights virgin 038a44a4b[/URL] [URL=http://787.galician.az.pl]charles town races and slots in charles town, west virginia 9e44[/URL] [URL=http://565.galician.az.pl]wheel of fortune slots for computer 64e[/URL] [URL=http://493.synchronize.az.pl]cheap business flights dublin sydney fdebd03[/URL] [URL=http://776.chinchillidae.az.pl]cheap flights from london to malaga 4c73916[/URL] [URL=http://32.chinchillidae.az.pl]cheap flights to dalaman 22978a4[/URL] [URL=http://657.chinchillidae.az.pl]cheap american domestic flights b27b6ee3[/URL] [URL=http://454.chinchillidae.az.pl]shaheen international flights schedule f7c3a5[/URL] [URL=http://121.chinchillidae.az.pl]cheap flights england to germany c125484[/URL] [URL=http://778.chinchillidae.az.pl]british airways flights to kolkata 141231[/URL] [URL=http://754.chinchillidae.az.pl]cheap flights to spain july 2007 188bc032[/URL] [URL=http://460.galician.az.pl]"charlestown races and slots, wv" 7b1b74e9e[/URL] [URL=http://156.chinchillidae.az.pl]larnaca to uk flights 11f1caa7d[/URL] [URL=http://698.galician.az.pl]liner slots online 5f1fbda4[/URL] [URL=http://336.galician.az.pl]free multi line bonus slots casino style 5b504df2[/URL] [URL=http://269.synchronize.az.pl]delta airlines baggages policy for international flights c9e4be0[/URL]

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
Comment:

Code:* Code

Powered by AkoComment Tweaked Special Edition v.1.4.6
AkoComment © Copyright 2004 by Arthur Konze - www.mamboportal.com
All right reserved

 
< 前へ   次へ >

© 2008 Labs Zsrv Net
Joomla! is Free Software released under the GNU/GPL License.
Translation is Joomla!JAPAN