SOAP Webservice函数仅获取第一个值

时间:2015-10-30 14:26:36

标签: php json web-services soap

我必须创建一个Web服务而且我没有这方面的经验,所以我使用SOAP和我在网上找到的一些代码。 Web服务应该在函数上获得三个值,两个是字符串,一个是带有数据的JSON,我将解码以插入mySQL。

但我有以下错误,我的函数只获得第一个值,JSON可以作为一个简单的字符串吗? 最后为什么utf8()给我错误?我试图这样做,因为它是一个西班牙语数据,我们有重音符号可能会破坏json解码。打印值时,所有函数变量都会获得我传递的第一个变量。

这是SERVER PHP代码:

<?php
if(!extension_loaded("soap")){
      dl("php_soap.dll");
}

require_once '../modulos/conexion.php';//connects to DB

function net_stat() {
    $msj = "Web Service en linea";
    return($msj);
}

function net_receive($token, $tabla, $json_encode) {
    $val_token = '8mp8';
    $stat = 'TOKEN: ' .$token.'<br>tabla: '.$tabla.'<br>Json: <br>'. $json_encode;
    //$json_datos = utf8($json_encode);<--Commented because throws unknown error
    //$json_query = json_decode($json_datos, true);
    $json_query = json_decode($json_encode, true);
    if ($val_token == $token) {
        switch ($tabla) {
            case 'cab_usuarios':
                cab_usuarios($json_query);
                $receiver_msg = 'Query realizado a $tabla';
                break;
            default:
                $receiver_msg = $tabla . ' no es un nombre de tabla valido';
                break;
        }
    } else {
        $receiver_msg = 'Token de autenticacion no valido';
    }
    cerrar_conexion_sql();
    $receiver_msg = $stat . '<br>' .$receiver_msg;
    return $receiver_msg;
}

function cab_usuarios($json_query) {
    foreach ($json_query as $row) {
        $id_usua = $row["id_usua"];
        $co_usua = $row["co_usua"];
        $no_usua = $row["no_usua"];
        $ds_pass = password_hash($row["ds_pass"], PASSWORD_BCRYPT);
        $st_usua = $row["st_usua"];
        $fe_ingre = $row["fe_ingre"];
        $co_user = $row["co_user"];
        $eTInf = $row["eTInf"];
        $aTSuc = $row["aTSuc"];
        $aTInf = $row["aTInf"];
        $co_vende = $row["co_vende"];
        $query = "INSERT INTO dbfar_cabusuarios (`id_usua`, `co_usua`, `no_usua`, `ds_pass`, `st_usua`, `fe_ingre`, `co_user`, `eTlnf`, `aTSuc`, `aTlnf`, `co_vende`) VALUES ($id_usua, $co_usua, $no_usua, $ds_pass, $st_usua, $fe_ingre, $co_user, $eTInf, $aTSuc, $aTInf, $co_vende)";
        mysql_query($query);
    }
}

ini_set("soap.wsdl_cache_enabled", "0");

$server = new SoapServer('farletza.wsdl');

$server->addFunction("net_stat");
$server->addFunction("net_receive");

$server->handle();

这是CLIENT PHP代码

<?php
// turn off the WSDL cache
ini_set("soap.wsdl_cache_enabled", "0");

$cliente = new SoapClient("http://localhost:8080/farletza/svc/farletza.wsdl");

$estadowebsvc = $cliente->net_stat();
echo "<strong>".$estadowebsvc."</strong><br/>";

$token = "8mp";
$tabla = "cab_usuarios";
//$json = "json";
$json = '[{"id_usua": "1","co_usua": "003", "no_usua": "jonathan", "ds_pass": "1234", "st_usua": "A", "fe_ingre":"12-04-2015", "co_user": "FARM", "eTInf": "0", "aTSuc": "2", "aTInf": "52EX", "co_vende": "Hernán"}]';

$conexion_remota = $cliente->net_receive($token, $tabla, $json);
echo $conexion_remota;

这是WSDL}

<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Farletza' 
  targetNamespace='http://localhost:8080/farletza/svc/farletza.wdsl' 
  xmlns:tns='http://localhost:8080/farletza/svc/farletza.wdsl' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

<message name='net_statRequest'> 
  <part name='symbol' type='xsd:string'/> 
</message> 
<message name='net_statResponse'> 
  <part name='Result' type='xsd:string'/> 
</message> 
<message name='net_receiveRequest'> 
  <part name='symbol' type='xsd:string'/>
  <part name='symbol' type='xsd:string'/>
  <part name='symbol' type='xsd:string'/>
</message> 
<message name='net_receiveResponse'> 
  <part name='Result' type='xsd:string'/> 
</message> 

<portType name='FarletzaPortType'> 
  <operation name='net_stat'>
    <input message='tns:net_statRequest'/> 
    <output message='tns:net_statResponse'/>   
  </operation>
  <operation name='net_receive'>
    <input message='tns:net_receiveRequest'/> 
    <output message='tns:net_receiveResponse'/>   
  </operation>    
</portType> 

<binding name='FarletzaBinding' type='tns:FarletzaPortType'> 
  <soap:binding style='rpc' 
    transport='http://schemas.xmlsoap.org/soap/http'/> 
  <operation name='net_stat'> 
    <soap:operation soapAction='urn:localhost:8080-farletza#net_stat'/> 
    <input> 
      <soap:body use='encoded' namespace='urn:localhost:8080-farletza' 
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
    </input> 
    <output> 
      <soap:body use='encoded' namespace='urn:localhost:8080-farletza' 
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
    </output> 
  </operation>
  <operation name='net_receive'> 
    <soap:operation soapAction='urn:localhost:8080-farletza#net_receive'/> 
    <input> 
      <soap:body use='encoded' namespace='urn:localhost:8080-farletza' 
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
    </input> 
    <output> 
      <soap:body use='encoded' namespace='urn:localhost:8080-farletza' 
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
    </output> 
  </operation>       
</binding> 

<service name='FarletzaService'> 
  <port name='FarletzaPort' binding='FarletzaBinding'> 
    <soap:address location='http://localhost:8080/farletza/svc/farletza_server.php'/> 
  </port> 
</service>
</definitions>

这是我在客户端获得的回报:

Web Service en linea
TOKEN: 8mp
tabla: 8mp
Json: 
8mp
Token de autenticacion no valido

可以看出,所有变量都具有相同的值,为什么会发生这种情况呢?

1 个答案:

答案 0 :(得分:0)

如果有人来到这里并且想知道我的错误,那就是在WSDL上我输入消息的部分名称相同,所以我在这里

<message name='net_receiveRequest'> 
  <part name='symbol' type='xsd:string'/>
  <part name='symbol' type='xsd:string'/>
  <part name='symbol' type='xsd:string'/>
</message> 

应该是

<message name='net_receiveRequest'> 
  <part name='symbol1' type='xsd:string'/>
  <part name='symbol2' type='xsd:string'/>
  <part name='symbol3' type='xsd:string'/>
</message>

因此它有3个独立的值。一个非常简单的错误。