php soap带验证

Server端

<?php
class Server {
    private $authenticated = false;

    public function auth($authcode) {
        if($authcode === '123456789') {
            $this->authenticated = true;
            return true;
        } else {
            throw new SoapFault('403', '403 Forbidden');
        }
    }

    public function getData() {
        if(!$this->authenticated) {
            throw new SoapFault('403', '403 Forbidden');
        }
        $data = array(
            'text' => 'ni mei a'
        );
        return json_encode($data);
    }
}

/*//生成wsdl文件
include("SoapDiscovery.class.php");

$disco = new SoapDiscovery('Server','Server');

$handle = fopen('server.wsdl', 'w+');
fwrite($handle, $disco->getWSDL());
fclose($handle);
*/


$objSoapServer = new SoapServer("server.wsdl");
// $objSoapServer->setClass("Server");
$serv = new Server;
$objSoapServer->setObject($serv);
$objSoapServer->handle();

Client端

<?php
try {
    $client = new SoapClient('server.wsdl',array("exceptions" => 1));

    $authvalues = new SoapVar(array('authcode' => '123456789',), SOAP_ENC_OBJECT);

    $header = new SoapHeader('urn:Server', 'auth', $authvalues, false, SOAP_ACTOR_NEXT);

    $client->__setSoapHeaders(array($header));
    $data = $client->getData();

    var_dump($data);
} catch (SoapFault $e) {
    echo $e->faultstring;
}

标签: none

添加新评论