par
mohi » 27 janv. 2016, 11:14
Bonjour,
Merci pour ta réponse.
Après plus d'investigation, je me rends compte que j'ai peut-être mal fait mon Web Service car je n'ai pas de WSDL. J'ai un client.php un server.php et un service.php qui est appelé.
Penses-tu qu'il est préférable de faire un WSDL ? Je souhaiterais retourner ma réponse en format XML. Si oui, aurais-tu un tuto à me conseiller ?
1°) Pour l'authentification, je souhaiterais passer le login/mdp dans un header. Connais-tu un tuto pour ca ?
2°) D'accord pour le HTTPS, je paye un serveur pour ça.
3°) Pour l'IP, j'ai fait un petit test en local qui affiche l'ip :
Ca me retourne : "::1". Cette variable ne fonctionne pas en local ?
Pour être plus clair, voici mon code qui renvoi le nom d'une personne en fonction de son id :
client.php :
Code : Tout sélectionner
<?php
class client{
public function __construct(){
$params = array('location' => 'http://localhost/webservice/server.php',
'uri' => 'urn://localhost/webservice/server.php',
'trace' => 1);
$this->instance = new SoapClient(NULL, $params);
//set the header
$auth_params = new stdClass();
$auth_params->username = 'name';
$auth_params->password = 'password';
$header_params = new SoapVar($auth_params, SOAP_ENC_OBJECT);
$header = new SoapHeader('codev', 'authenticate', $header_params, false);
$this->instance->__setSoapHeaders(array($header));
}
public function getName($name_array){
return $this->instance->__soapCall('getName', $name_array);
}
}
$client = new client;
?>
server.php :
Code : Tout sélectionner
<?php
class server{
private $con;
public static function authenticate($header_params){
if($header_params->username == 'name' && $header_params->password == 'password') return true;
else throw new SOAPFault('Wrong user/pass combination', 401);
}
public function __construct(){
$this->con = (is_null($this->con)) ? self::connect() : $this->con;
}
static function connect(){
$con = mysql_connect('localhost', 'root', '');
$db = mysql_select_db('soap', $con);
return $con;
}
public function getName($id_array){
$id = $id_array['id'];
$sql = "SELECT name FROM personne WHERE id = '$id'";
$qry = mysql_query($sql, $this->con);
$res = mysql_fetch_array($qry);
return $name_array;
}
}
$params = array('uri' => 'webservice/server.php');
$server = new SoapServer(NULL, $params);
$server->setClass('server');
$server->handle();
?>
service.php :
Code : Tout sélectionner
<?php
include './client.php';
$id= $_GET['id'];
$id_array = array('id' => $id);
echo "Hello " .$client->getName($id_array);
?>
Merci encore

Bonjour,
Merci pour ta réponse.
Après plus d'investigation, je me rends compte que j'ai peut-être mal fait mon Web Service car je n'ai pas de WSDL. J'ai un client.php un server.php et un service.php qui est appelé.
Penses-tu qu'il est préférable de faire un WSDL ? Je souhaiterais retourner ma réponse en format XML. Si oui, aurais-tu un tuto à me conseiller ?
1°) Pour l'authentification, je souhaiterais passer le login/mdp dans un header. Connais-tu un tuto pour ca ?
2°) D'accord pour le HTTPS, je paye un serveur pour ça.
3°) Pour l'IP, j'ai fait un petit test en local qui affiche l'ip :
[code]$ip = $_SERVER['REMOTE_ADDR'];
echo $ip ;[/code]
Ca me retourne : "::1". Cette variable ne fonctionne pas en local ?
Pour être plus clair, voici mon code qui renvoi le nom d'une personne en fonction de son id :
client.php :
[code]<?php
class client{
public function __construct(){
$params = array('location' => 'http://localhost/webservice/server.php',
'uri' => 'urn://localhost/webservice/server.php',
'trace' => 1);
$this->instance = new SoapClient(NULL, $params);
//set the header
$auth_params = new stdClass();
$auth_params->username = 'name';
$auth_params->password = 'password';
$header_params = new SoapVar($auth_params, SOAP_ENC_OBJECT);
$header = new SoapHeader('codev', 'authenticate', $header_params, false);
$this->instance->__setSoapHeaders(array($header));
}
public function getName($name_array){
return $this->instance->__soapCall('getName', $name_array);
}
}
$client = new client;
?>[/code]
server.php :
[code]<?php
class server{
private $con;
public static function authenticate($header_params){
if($header_params->username == 'name' && $header_params->password == 'password') return true;
else throw new SOAPFault('Wrong user/pass combination', 401);
}
public function __construct(){
$this->con = (is_null($this->con)) ? self::connect() : $this->con;
}
static function connect(){
$con = mysql_connect('localhost', 'root', '');
$db = mysql_select_db('soap', $con);
return $con;
}
public function getName($id_array){
$id = $id_array['id'];
$sql = "SELECT name FROM personne WHERE id = '$id'";
$qry = mysql_query($sql, $this->con);
$res = mysql_fetch_array($qry);
return $name_array;
}
}
$params = array('uri' => 'webservice/server.php');
$server = new SoapServer(NULL, $params);
$server->setClass('server');
$server->handle();
?>[/code]
service.php :
[code]<?php
include './client.php';
$id= $_GET['id'];
$id_array = array('id' => $id);
echo "Hello " .$client->getName($id_array);
?>[/code]
Merci encore :)