PHP操作Webservice

Posted 方白衣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP操作Webservice相关的知识,希望对你有一定的参考价值。

Function:

//server:
<?php
$soap = new SoapServer(null,array(‘uri‘=>"http://192.168.1.110/"));    //This uri is your SERVER ip.
$soap->addFunction(‘minus_func‘);                                                 //Register the function
$soap->addFunction(SOAP_FUNCTIONS_ALL);
$soap->handle();

function minus_func($par){
    return "Hello,".$par;
}
?>

//client:
<?php
try {
    $client = new SoapClient(null,
        array(‘location‘ =>"http://192.168.1.110/server.php",‘uri‘ => "http://192.168.1.110/"));
    echo $client->minus_func(‘fangbaiyi‘);

} catch (SoapFault $fault){
    echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring;
}
?>

Class:

//server:
<?php
    //$classExample=array();
    $soap=new SoapServer(null,array(‘uri‘=>"http://192.168.1.110"));
    $soap->setClass(‘chClass‘);
    $soap->handle();
    
    class chClass
    {
        public $mes="Hello World!";
        function getName()
        {
            return $this->mes;
        }
    }
?>

//client:
<?php
try{
    $client=new SoapClient(null,array(‘location‘=>"http://192.168.1.110/server1.php",‘uri‘=>"http://192.168.1.110"));
    echo $client->getName();
}catch(SoapFault $fault)
{
    echo $fault;
}
?>

以上是关于PHP操作Webservice的主要内容,如果未能解决你的问题,请参考以下文章

php webservice

php调用webservice接口,java代码接收不到参数

PHP写webservice服务端

超级有用的9个PHP代码片段

php调用webservice接口

PHP webservice 的初接触