Home > Article > Backend Development > php soap usage
Preparation:
Be prepared before using soap. Create a file in the www directory and add the following content under the file.
<?php echo phpinfo();?>run this file to see if there is a soap module. If not, open the php.ini file. , remove the ; in front of
extension=php_soap.dll
, and restart apache.
Client:
Create a new php file in the www directory, the file name is client.php, add the following content under the file
<?php $client = new SoapClient(null, array('location' => "http://www.samples.com/Service.php",//服务端的文件位置 'uri'=> "","login" => "outsider","password" => "1234567"//访问服务端文件时的用户名和密码)); $user_info = json_decode($client->user_info());//服务端的方法 $result = $client->show(1,3);//服务端的方法 print_r($user_info->email.".....".$result); ?>Server:
Create a new php file in the www directory, the file name Service.php, add the following content under this file
<?php if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !($_SERVER['PHP_AUTH_USER'] == 'outsider' && $_SERVER['PHP_AUTH_PW'] == '1234567')) { header('WWW-Authenticate: Basic realm="WEBSERVICE"'); header("HTTP/1.0 401 Unauthorized"); echo "You must enter a valid login ID and password to access this resource/n"; die; }//验证用户名和密码 class fuck{ function show($one, $two) { return $one + $two; } function user_info() { $user_info = array( 'name' => 'Outsider', 'sex' => '男', 'email' => 'outsider@outsiderla.me', 'tel' => '1369*******', ); return json_encode($user_info); } } $server = new SoapServer(null, array('uri' => '', 'location' => 'http://www.samples.com/soapService.php')); $server->setClass('fuck');//注册fuck类 $server->handle(); ?>. This way, the server and client are finished. Now access the client. The result is
The above introduces the usage of php soap, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.