本人所了解的webservice有以下几种:PHP本身的SOAP,开源的NUSOAP,商业版的PHPRPC,以及使用二进制传输数据流的HessianPHP,那么一下就简单的介绍下这几种webservice在php中的使用,虽然网上也有很多这方面的资料,但是这是我个人实践所得,当然也是从网上找的资料,在此简单的做个笔记。
一:PHP本身的SOAP
所有的webservice都包括服务端(server)和客户端(client)。
要使用php本身的soap首先要把该拓展安装好并且启用。下面看具体的code
首先这是服务端实现:
PHP代码
<?php class test { function show() { return 'the data you request!'; } } function getUserInfo($name) { return 'fbbin'; } //实例化的参数手册上面有,这个是没有使用wsdl的,所以第一个参数为null,如果有使用wsdl,那么第一个参数就是这个wsdl文件的地址。 $server = new SoapServer(null, array('uri' ='http://soap/','location'='http://localhost/test/server.php')); $server->setClass('test'); //$server->addFunction('getUserInfo'); $server->handle(); ?>
然后是客户端
PHP代码
$soap = new SoapClient(null, array('location'='http://localhost/test/server.php','uri' ='http://soap/')); echo $soap->show(); //得到:'the data you request!' //echo $soap->getUserInfo('sss');
就这么简单,当时这只是一个很简单的例子,其实很多的通信机制都是这么去实现的!
////////////////////////////////////////////////////////////////////////////////
二:PHPRPC
首先到官网(http://www.phprpc.org/zh_CN/ )上面去下载最新版的phprpc,解压之后会有相关的文件,我们把文件进行划分(服务端和客户端文件)如下:
服务端文件:
PHP代码
dhparams dhparams.php phprpc_server.php bigint.php compat.php phprpc_date.php xxtea.php
客户端文件:
PHP代码
phprpc_client.php bigint.php compat.php phprpc_date.php xxtea.php
我们把服务端文件放在服务端文件夹中,然后把客户端文件放在客户端文件夹中,之后再服务端文件夹中新建个文件(server.php)作为服务,然后再客户端新建个文件(client.php)作为客户端,各自代码如下:
server端:
PHP代码
<?php include_once"phprpc_server.php"; //加载phprpc文件 $server = new PHPRPC_Server(); $server->add('getUser'); $server->setDebugMode(true); $server->start(); function getUser( ) { return ‘the data you request!’; }
client端:
PHP代码
<?php include_once "phprpc_client.php"; $client = new PHPRPC_Client('http://127.0.0.1/phpservice/phprpcserver/server.php'); $data = $client->getUser(); var_dump($data); //得到:the data you request!
这上面提到wsdl之后会讲到如何生成。
三:开源的NUSOAP
首先到网上去下载最新版的nusoap,现在的最新版本是0.9.5的,解压之后会得到一个lib文件夹,把这个文件分别放到服务端和客户端各一份,然后再服务端和客户端分别建立server.php和client.php文件,作为通信文件。
服务端文件如下:
PHP代码
<?php ini_set("soap.wsdl_cache_enabled", 0);//关闭缓存 require_once("lib/nusoap.php"); //加载nusoap文件 $server = new soap_server; $server->configureWSDL('nusoasp');//设定服务的名称,使用的wsdl来通信,如果不适用wsdl将会更简单,网上有很多的例子 $server->register('getUserInfo', array('name'="xsd:string", 'email'="xsd:string"), array('return'="xsd:string")); $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service( $HTTP_RAW_POST_DATA ); function getUserInfo($name, $email) { return ‘the data you request!’; }
客户端文件如下:
PHP代码
require_once("lib/nusoap.php"); $client = new soapclient('http://localhost/phpservice/nusoapserver/server.php?wsdl'); $pagram = array('fbbin', 'fbbin@foxmail.com'); $string = $client->call('getUserInfo', $pagram); //得到:the data you request!
四:HessianPHP
hessian其实我个人认为他不是一个webservice,只能说是类似而已。因为它不具备webservice的那些特性。它支持的语言比较多我们现在只需要研究php版本的HessianPHP就行了,下载最新版本是v2.0.3的,解压之后会得到一个src的目录,这个目录使我们需要使用的一个核心文件夹。
我们把名字重命名为HessianPHP然后分别分别放到server和client端,然后分别建立server.php和client.php文件。
server端:
PHP代码
<?php include_once 'HessianPHP/HessianService.php';//加载核心文件 class TestService { public function __construct() { } public function add($numa, $numb) { return $numa + $numb; } public function check() { return 'fbbiin@gmail.com'; } } $test = new TestService(); $hessian = new HessianService( $test, array('displayInfo' => true) ); $hessian->handle();//注意这里不是网上的$hessian->service(),可能是版本不一样,改了吧!我也是看了源码才知道! ?>
client 端:
PHP代码
<?php include_once 'HessianPHP/HessianClient.php'; $url = "http://localhost/phpservice/hessianserver/server.php"; $options = new HessianOptions(); $client = new HessianClient( $url, $options ); $num = $client->add( 3, 5 ); echo $num;//得到:8; echo $client->check();//得到:fbbiin@gmail.com;
以上四种方式为web开发过程中常用到的webservice通信方式。用的最多的是nusoap,个人感觉phprpc其实也不错,这个在性能上面和nusoap基本上差不多,只不过,phprpc在商业上是收费的。还有个hessianPHP好像是用java的,采用的二进制方式传输数据流,其实也是各有千秋啊。更多的详细资料去找百度和谷歌吧。
下面说下生成wsdl文件。
我们在webservice上面进行通信用的最多的和相对来说比较安全的就是使用wsdl了,这种文件可以自己书写,但是不是一定的大牛好像写不出来啊,因此我们需要借助一个工具zend studio来生成wsdl文件。
下面我们就来生成WSDL文件了,File->New—>Other—>Web Service—>WSDL,这样就可以新建一个WSDL文件了,如图。
然后我们就来修改WSDL文件,zeng studio为我们提供了可视化的操作,当然如果你牛的话,你当然是可以改文件代码的,其实也就几个东西, 弄懂了的话也不会太难。
做完这一步,这个WSDL文件就基本可用了,但又两个问题需要注意:
做到这一步,有可能会测试失败,可能会因为没有进行binding,这个东西有时是需要手动来完成的,在binding上右键选择Generate Binding Content(就是两个大框中间那个小框)就行了。
第二个要注意的是php的WSDL缓存,在做测试时,一般要将WSDL缓存关闭,否则你使用的有可能是原来的WSDL文件,而不是更新过的。关闭缓存有两种方法,第一种就是直接到php.ini中设置soap.wsdl_cache_enabled = 0;第二种就是在php文件中添加一条语句,ini_set("soap.wsdl_cache_enabled", "0");
做到这里,你就可以放心地测试,调用你的server程序了。
说完了,OK!

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Alipay PHP...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),