以前发的问题:
1、http://www.thinkphp.cn/topic/649.html
2、http://www.thinkphp.cn/topic/1831.html
下面分享一下我实现的代码:
第一步:创建一个服务器端程序server.php
<?php
class sunline {
public function api($params){
if (empty($params['module'])) return "";
//这里可以加些验证规则,实现一些特殊目的。如安全方面的。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://你的路径/index.php?m=".$params['module']."&a=".$params['action']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$output = curl_exec($ch);
if ($output === FALSE) {
return "API Error: " . curl_error($ch);
}
$info = curl_getinfo($ch);
curl_close($ch);
return array( 'result'=>$output,'info'=>$info);
}
public function isConnect(){
return "_______OK!";
}
}
$s = new SoapServer(null,array("location"=>"http://localhost/soap/server.php", 'uri'=>'server.php'));
$s -> setClass("sunline");
$s -> handle();第二步:创建一个客户端请求测试文件client.php<?php
try{
$soap = new SoapClient( null, array('location'=>"http://localhost/soap/server.php", 'uri'=>'server.php') );
$result2 = $soap->__soapCall('isConnect',array() );
$result4 = $soap->__soapCall('api',array(array('module'=>'Soap','action'=>'getName','user'=>'ljs', 'password'=>'liaojianshan', 'id'=>5)) );
echo $result2;
echo "<pre>";
print_r($result4);
echo "</pre>";
}catch(SoapFault $e){
echo $e->getMessage();
}catch(Exception $E){
echo $E->getMessage();
}成功!注意:这个server.php并不需要在thinkphp的项目中,可以放在任何服务器上。
以上应用请各位大神斧正!
最佳答案