• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
              C++博客 :: 首頁 :: 新隨筆 ::  ::  :: 管理
            http://ian-jiang.iteye.com/blog/664727

            剛在項(xiàng)目中應(yīng)用到了java與php通過webservice進(jìn)行數(shù)據(jù)交互。覺得挺有意思,貼出來,跟大家分享。

            一.java編寫webservice服務(wù)端,php作為客戶端調(diào)用.

            1.首先我們寫一個(gè)簡單的java類并發(fā)布webservice.

            package com.php;

            import java.util.Map;

             

            public class WebServiceImpl {

             public String sendTransact(Map map) throws Exception {
              System.out.println("::: Call testModel1 :::");
              
              if(map!=null){
               String bugmanifestid = StringUtil.getValue(map.get("bugmanifestid"));
               String editedby = StringUtil.getValue(map.get("editedby"));
               String dditeddate = StringUtil.getValue(map.get("dditeddate"));
               String fullinfo = StringUtil.getValue(map.get("fullinfo"));
               String action = StringUtil.getValue(map.get("action"));
               System.out.println("bugmanifestid ->" +bugmanifestid);
               System.out.println("editedby      ->" +editedby);
               System.out.println("dditeddate    ->" +dditeddate);
               System.out.println("fullinfo      ->" +fullinfo);
               System.out.println("action        ->" +action);
              }
              return "success";
             }
            }

             2.配置server-config.wsdd

            <deployment xmlns="http://xml.apache.org/axis/wsdd/"
             xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

             <handler name="URLMapper"
              type="java:org.apache.axis.handlers.http.URLMapper" />
             <handler name="auth"
              type="java:com.php.AuthenticationHandler" />
             <handler name="URLLogging"
              type="java:com.php.LogHandler">
              <parameter name="filename" value="c:\\MyService.log" />
             </handler>

             <service name="IWebService" provider="java:RPC">
              <parameter name="className"
               value="com.php.WebServiceImpl" />
              <parameter name="allowedMethods" value="*" />
              <namespace>http://localhost:8088/testphpweb</namespace>    
             </service>

             <transport name="http">
              <requestFlow>
               <handler type="URLMapper" />
               <handler type="URLLogging" />
              </requestFlow>
             </transport>

            </deployment>

            3.發(fā)布到j(luò)boss后,訪問http://localhost:8088/testphpweb/services/IWebService?wsdl能看到xml文件就說明webservice發(fā)布好了。

            4.寫testphpweb.php文件

             <?php

            header("Content-Type: text/html; charset=GB2312");
            echo " ::: PHP CALL JAVA-WEBSERVICE ::: <br>";
            require_once("nusoap/lib/nusoap.php");

            // 要訪問的webservice路徑
            $NusoapWSDL="http://localhost:8088/testphpweb/services/IWebService?wsdl";

            // 生成客戶端對(duì)象
            $client = new soapclient($NusoapWSDL, true);

            // 設(shè)置參數(shù)(注意:PHP只能以'數(shù)組集'方式傳遞參數(shù),如果服務(wù)端是java,用Map接收)
            $param = array( 'bugmanifestid' => 'E090500001',
                'editedby'      => '張三',
                'dditeddate'    => '2009-05-19',
                'fullinfo'      => '已聯(lián)系劉德華,籌備今晚吃飯的事,等待回復(fù)',
                'action'        => '0');

            echo "begin remote 。。。<br>";
            // 調(diào)用遠(yuǎn)程方法
            $result = $client->call('sendTransact', array($param));
            echo "end remote 。。。<br>";

            // 顯示執(zhí)行結(jié)果
            if (!$err=$client->getError()){  
             echo '結(jié)果 : '.$result;    
            }else{  
             echo '錯(cuò)誤 : '.$err;  
            }  
            ?>

             5.啟動(dòng)apache,訪問http://localhost/service/testphpweb.php

            php頁面顯示:

             ::: PHP CALL JAVA-WEBSERVICE :::
            begin remote 。。。
            end remote 。。。
            結(jié)果 : success

            jboss后臺(tái)監(jiān)視結(jié)果:

            17:12:20,781 INFO  [STDOUT] ::: Call testModel1 :::
            17:12:20,781 INFO  [STDOUT] bugmanifestid ->E090500001
            17:12:20,781 INFO  [STDOUT] editedby      ->張三
            17:12:20,781 INFO  [STDOUT] dditeddate    ->2009-05-19
            17:12:20,781 INFO  [STDOUT] fullinfo      ->已聯(lián)系劉德華,籌備今晚吃飯的事,等待回復(fù)
            17:12:20,796 INFO  [STDOUT] action        ->0

            到此,php作為客戶端調(diào)用java寫的webservice服務(wù)端完成.

            二,php編寫webservice服務(wù)端,java作為客戶端調(diào)用.

            1.編寫php webservice    

            <?php

            header("Content-Type: text/html; charset=GB2312");
            require_once("nusoap/lib/nusoap.php");

            function sendManifest($param)
            {

              //把接收到的數(shù)據(jù)顯示出來
                return "hello ".$param["projectid"]."<=>".$param["projectname"]."<=>".$param["moduleid"];
            }

            $server = new nusoap_server();

            //配置WSDL namespace
            $server->configureWSDL('myservice',                                    //服務(wù)名稱
                 'http://localhost/service/web_call_center.php',                   //tns指定的namespace,一般填寫自己的URI
                 true,                                                             //endpoint url or false
                 'rpc',                                                            //服務(wù)樣式
                 'http://schemas.xmlsoap.org/soap/http',                           //傳輸協(xié)議,一直是這個(gè)。
                 'http://localhost/service/web_call_center.php'                    //wsdl 'types'元素targetNamespace
            );

            // 注冊(cè)web服務(wù)
            $server->register('sendManifest',                                      // 服務(wù)
                array(
             'projectid'     => 'xsd:string',
             'projectname'   => 'xsd:string',
             'moduleid'      => 'xsd:string',
             'modulepath'    => 'xsd:string',
             'bugtitle'      => 'xsd:string',
             'bugtype'       => 'xsd:string',
             'openedby'      => 'xsd:string',
             'openeddate'    => 'xsd:string',
             'assignedto'    => 'xsd:string',
             'assigneddate'  => 'xsd:string',
             'fixedtime'     => 'xsd:string',
             'fullinfo'      => 'xsd:string',
             'bugmanifestid' => 'xsd:string'),                                  // 輸入?yún)?shù);數(shù)組,指定類型
                array('resultCode' => 'xsd:string'),                               // 輸出;數(shù)組,指定類型
                'http://localhost/service/web_call_center.php',                    // namespace of method
                '',                                                                // soapaction
                'rpc',                                                             // style
                'encoded',                                                         // use
                'serviceConsumeNotify'                                             // documentation
            );

            $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
            $server->service($HTTP_RAW_POST_DATA);
            ?>

            2.啟動(dòng)apache后,訪問  http://localhost/service/phpserver.php,如果頁面如下圖所示,表示webservice發(fā)布好了。
             

             3.編寫java客戶端CallPhpServer .java 并調(diào)用php webservice

            package com.php;

            import java.util.HashMap;
            import java.util.Map;
            import org.apache.axis.client.Call;
            import org.apache.axis.client.Service;

             

            public class CallPhpServer {

             
             public static String callManifest() throws Exception {
              System.out.println("0");
              Service service = new Service();

              Call call = (Call) service.createCall();
              System.out.println("1");
              call.setTargetEndpointAddress(new java.net.URL("http://localhost/service/phpserver.php")); 
              call.setOperationName("sendManifest");
              System.out.println("2");
              Map map=new HashMap();
              map.put("projectid", "109");
              map.put("projectname", new String("新MM國際物流平臺(tái)".getBytes(),"iso-8859-1"));
              map.put("moduleid", "11");
              map.put("modulepath", new String("財(cái)務(wù)管理".getBytes(),"iso-8859-1"));
              map.put("bugtitle", new String("關(guān)于總賬報(bào)表數(shù)據(jù)的問題".getBytes(),"iso-8859-1"));
              map.put("bugtype", "TrackThings");
              map.put("openedby", "zhangsan");
              map.put("openeddate", "2009-05-31");
              map.put("assignedto", "liumang");
              map.put("assigneddate", "2009-05-31");
              map.put("fixedtime", "2009-06-03");
              map.put("fullinfo", new String("現(xiàn)在總賬報(bào)表頁面下的合計(jì)數(shù)據(jù)不對(duì),煩請(qǐng)抓緊事件核實(shí)確認(rèn)更正,謝謝!".getBytes(),"iso-8859-1"));
              map.put("bugmanifestid", "E090500001");
              call.addParameter("param", org.apache.axis.Constants.SOAP_ARRAY,javax.xml.rpc.ParameterMode.IN);
              call.setReturnType(org.apache.axis.Constants.XSD_STRING);
              System.out.println("3");
              Object obj=call.invoke(new Object[]{map});
              return obj.toString();
             }
             public static void main(String[] args) throws Exception {
              System.out.println("::: call php webservice :::");
              String str = callManifest();

              String result=new String(str.getBytes("iso-8859-1"),"GBK");
              System.out.println(result);
             }
            }
            控制臺(tái)顯示結(jié)果:

            ::: call php webservice :::
            0
            log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
            log4j:WARN Please initialize the log4j system properly.
            1
            2
            3
            hello 109<=>新MM國際物流平臺(tái)<=>11
            到此,java作為客戶端調(diào)用php的webservice服務(wù)端完成.

            91久久精品91久久性色| 亚洲国产成人精品女人久久久 | 国产精品亚洲美女久久久| 国产精品天天影视久久综合网| 成人资源影音先锋久久资源网| 亚洲国产成人久久综合碰碰动漫3d| 久久99精品国产麻豆蜜芽| 久久无码AV一区二区三区| 无码人妻久久一区二区三区| 国产激情久久久久影院老熟女免费| 中文字幕无码久久精品青草| 久久国产精品一区二区| 中文字幕热久久久久久久| 91久久精品国产成人久久| 99久久综合国产精品免费| 国产成人综合久久久久久 | 99久久免费国产特黄| 久久久久无码精品| 久久免费视频网站| 性做久久久久久久| 伊人久久五月天| 久久精品国产只有精品66| 韩国三级大全久久网站| 午夜精品久久久久久中宇| 要久久爱在线免费观看| 91久久香蕉国产熟女线看| 国产99精品久久| 97久久国产亚洲精品超碰热| 人妻丰满AV无码久久不卡| 午夜欧美精品久久久久久久| 亚洲精品乱码久久久久66| 久久91精品国产91| 久久精品极品盛宴观看| 久久久久亚洲?V成人无码| 久久久国产精华液| 精品国产一区二区三区久久蜜臀| 精品久久久久久亚洲| 91精品国产91久久久久久蜜臀| 久久国产精品久久精品国产| 国产成人综合久久精品尤物| 久久久久久A亚洲欧洲AV冫 |