PHP编写和调用WebService

用PHP可以实现基本的WebService功能,本文给出了用PHP编写WebService、用PHP编写代码调用WebService以及用Delphi调用WebService的示例代码。
编写WebService服务器端文件:soapserver.php

<?php
  require_once ´nusoap.php´;
  $soap = new soap_server;
  $soap->configureWSDL(´WSDataFix´, ´http://www.shangsi.com.cn/´);
  //$soap->wsdl->schemaTargetNamespace = ´http://soapinterop.org/xsd/´;
  $soap->register(´add´, array(´a´ => ´xsd:int´, ´b´ => ´xsd:int´), array(´c´ => ´xsd:int´, ´d´ => ´xsd:int´), ´http://www.shangsi.com.cn/´ );
  $soap->service(isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ´´);
  function add($a, $b, $c, $d) {
    $c = $a * $b;
    $d = $a + $b;
    return array(´c´ => $c, ´d´ => $d);
  }
?>

 

编写WebService客户端调用:

<?
include(´nusoap.php´);
$client1 = new soapclient(´http://yourserver/nusoap_server3.php´);
$n1 = 465;
$n2 = 14;
$params2 = array(´a´=>$n1, ´b´=>$n2);
$ccc = $client1->call(´add´, $params2);
print_r($ccc);
?>

Delphi调用代码: 

var
  s,s1:widestring;
  i,j,k,l,m,n:integer;
  ss:AOS;
begin
  HTTPRIO := THTTPRIO.Create(self);
  HTTPRIO.WSDLLocation := ´http://www.shangsi.com.cn/ascdl/nusoap_server3.php?wsdl´;
  i := 465;
  j := 14;
  k := 2;
  l := 3;
  s1 := ´This string will be reversed´;
  (HTTPRIO as WSDataFixPortType).add(i, j, k, ss);  TntEdit1.Text := ss[0];
  edit1.Text := ss[1];
end;

发表评论