Home >Backend Development >PHP Tutorial >js access php using webservice

js access php using webservice

WBOY
WBOYOriginal
2016-07-29 08:59:101314browse
客户端:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>js客户端</title>
    <style type="text/css">
         *{ margin: 0; padding: 0; font-family: "宋体";}
        .box{ width: 500px; height: 200px; border: 1px solid blue; margin: 0 auto; }
        .msg{ width: 100%; height: 40px; line-height: 40px; background-color: #000033; color: #fff2de; display: inline-block;}
        input{ width: 300px; height: 30px; line-height: 30px; border: 1px solid green; margin-top: 10px; margin-bottom: 10px;}
         button{ width:300px; height: 35px;line-height: 35px; border: 1px solid red; margin-left:60px; }
         button:hover{ cursor: pointer;}
    </style>
</head>
<body>
  <div class="box">
      <label class="msg">案例: 客户端输入用户名,向服务器请求数据</label>
      <label>用户名:</label> <input type="text" name="uname" class="uname"><br />
      <button type="button" class="butt" /button>
  </div>
<script type="text/javascript" src="jquery1.11.3.js"></script>
<script type="text/javascript" src="layer/layer.js"></script>
<script type="text/javascript">
 //得到当前的域名
     var _host=window.location.host;
     var _usernameVal; //用户名输入框的值
    //实现点击事件
     function getWebserviceData(){
         var _uname=$('.uname').val();
         if(_uname == '' || _uname == null){
             layer.alert('很抱歉!没有数据', {icon:5});
             return false;
         }else{
             var _url="http://127.0.0.1/php_app/nusoapClient.php";//api接口地址
             $.get(_url,{"uname":_uname,"call_type":'json'},function(data){
                        layer.msg(data);
                 });
            }
    }
</script>
</body>
</html>

PHP client:

nusoap client

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhanghua 
 * Date: 16-3-11
 * Time: 下午3:53
 * To change this template use File | Settings | File Templates.
 */
require_once ("nusoap/nusoap.php");
$url=&#39;http://127.0.0.1/php_app/nusoapService.php&#39;;
$client = new soapclient ($url);
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;
$client->xml_encoding = 'UTF-8';

//得到客户端的数据,将数据传递到service,在service中再去调用第三方文件 查询、操作业务逻辑并等待结果
// 参数转为数组形式传递
$paras = array ('username' =>$_GET['uname'],'calltype'=>$_GET['call_type']);
$result = $client->call ( 'getResponse', $paras ); //去
// 检查错误,获取返回值
if (! $err = $client->getError()) { //如果没有错误
      if($_GET['call_type'] == 'xml'){
          header('content-type: text/xml');
          echo $result;
      }elseif($_GET['call_type'] == 'json'){
          echo " 返回结果: ", $result;
      }
} else {
    echo " 调用出错: ", $err;
}

nusoap servier<pre name="code" class="php"><?php
/**
 * Created by JetBrains7 PhpStorm.
 * User: Administrator
 * Date: 16-3-11
 * Time: 下午3:52
 * To change this template use File | Settings | File Templates.
 */
require_once ("nusoap/nusoap.php");
$server=new soap_server();
// 避免乱码
$server->soap_defencoding = 'UTF-8';
$server->decode_utf8 = false;
$server->xml_encoding = 'UTF-8';
$server->configureWSDL ('test'); // 打开 wsdl 支持
/*
注册需要被客户端访问的程序
类型对应值: bool->"xsd:boolean"    string->"xsd:string"
int->"xsd:int"     float->"xsd:float"
*/
$server->register (
    'getResponse', // 注册需要访问的方法名
    array ("username" => "xsd:string" ), // 参数,默认为 "xsd:string"
    array ("return" => "xsd:string" ) ); // 返回值,默认为 "xsd:string"
//isset  检测变量是否设置
$HTTP_RAW_POST_DATA = isset ( $HTTP_RAW_POST_DATA ) ? $HTTP_RAW_POST_DATA : '';
//service  处理客户端输入的数据
$server->service ( $HTTP_RAW_POST_DATA );

function getResponse($username,$calltype){
    //查询数据
    //得到查询结果
    $array=array(
        'status'=>1,
        'info'=>array(
            'tel'=>'1234567890',
            'sex'=>'1',
            'addres'=>'xxxxxxxxxxxxxxxxx'
        )
    );
    if($calltype == 'json'){
        //将数据json 化
        return json_encode($array);
    }elseif($calltype == 'xml'){
        return arr2xml($array);
    }
}

/**
 * @param $arr
 * @param null $node
 * @return mixed
 *  数组转xml
 */
function arr2xml($arr,$node=null) {
    if($node === null) {
        $simxml = new simpleXMLElement('<?xml version="1.0" encoding="utf-8"?><root></root>');
    } else {
        $simxml = $node;
    }
    // simpleXMLElement对象如何增加子节点?
 foreach($arr as $k=>$v) {
        if(is_array($v)) {
            //$simxml->addChild($k);
            arr2xml($v,$simxml->addChild($k));
        } else if(is_numeric($k)) { //标签不能以数字开头,和变量类似
            $simxml->addChild('item' . $k,$v);
        } else {
            $simxml->addChild($k,$v);
        }
    }

    return $simxml->saveXML();
}

js访问php使用 webservice


The above introduces the use of webservice by js to access php, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn