Мне нужно создать веб-сервис, и у меня нет опыта с этим, поэтому я использую SOAP и некоторый код, который я нашел в Интернете. Предполагается, что веб-служба получит три значения для функции, две - строки, а одна - JSON с данными, которые я буду декодировать для вставки на mySQL.Функция SOAP Webservice, получающая только первое значение
Но у меня есть следующая ошибка, моя функция получает только первое значение и может ли 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();
Вот код КЛИЕНТ 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
Как видно, все переменные получают одинаковое значение, почему это происходит?
из-за 'if ($ val_token == $ token) {' where' $ val_token = '8mp8'', но вы отправляете '$ token =" 8mp "' ... – davidkonrad
@davidkonrad Я знаю, что токен не тот же значение, но я все еще получаю эту переменную на всех остальных. Проверка проверки токена происходит, но, кроме того, другие данные не отправляются. –