如何用php调用webservice

 一、利用php的soap扩展

( extension=php_openssl.dll         extension=php_soap.dll          php.ini 这2项要开通 )
利用http://webservice.webxml.com.cn/提供的获取ip地址的webservices

<?php
 $client = new SoapClient(“http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl“); 
 echo ‘<pre>’; 
 print_r($client->__getFunctions ()) ;// 获取webservice提供的函数 
 echo ‘</pre>’;     

 $myip =  $out->getGeoIPContextResult->string; 
 echo ‘<pre>’; 
 print_r($myip); 
 echo ‘</pre>’; 
   
 $param = array(‘theIpAddress’=>’192.168.1.1’);//注意参数 
 $out = $client->getCountryCityByIp($param); 
 $arr = $out->getCountryCityByIpResult->string; 
 echo ‘<pre>’; 
 print_r($arr); 
 echo ‘</pre>’; 
?>

输出的结果:

Array 
 ( 
     [0] => getCountryCityByIpResponse getCountryCityByIp(getCountryCityByIp $parameters) 
     [1] => getGeoIPContextResponse getGeoIPContext(getGeoIPContext $parameters) 
     [2] => getVersionTimeResponse getVersionTime(getVersionTime $parameters) 
     [3] => getCountryCityByIpResponse getCountryCityByIp(getCountryCityByIp $parameters) 
     [4] => getGeoIPContextResponse getGeoIPContext(getGeoIPContext $parameters) 
     [5] => getVersionTimeResponse getVersionTime(getVersionTime $parameters) 
     [6] => ArrayOfString getCountryCityByIp(string $theIpAddress) 
     [7] => ArrayOfString getGeoIPContext() 
     [8] => string getVersionTime() 
     [9] => ArrayOfString getCountryCityByIp(string $theIpAddress) 
     [10] => ArrayOfString getGeoIPContext() 
     [11] => string getVersionTime() 
 ) 
 Array 
 ( 
     [0] => 211.103.248.103 
     [1] => 北京市昌平区 电信通 
 ) 
 Array 
 ( 
     [0] => 192.168.1.1 
     [1] => 局域网 对方和您在同一内部网 
 )
 

二、服务器不支持soap扩展的情况下,可引入网上开源的类库:

nusoap下载地址:http://u.115.com/file/clwjpyb8

<?php
include(“lib/nusoap.php”);
 $client = new SoapClient(“http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl”,”wsdl“); 
 $client->soap_defencoding = ‘utf-8’; 
 $client->decode_utf8 = false; 
 $client->xml_encoding = ‘utf-8’; 
 $out = $client->call(‘getGeoIPContext’); 
   
 echo ‘<pre>’; 
 print_r($out) ; 
 echo ‘</pre>’; 
   
 $param = array(‘theIpAddress’=>’202.173.45.28’);//注意参数 
 $out = $client->call(‘getCountryCityByIp’,$param); 
 echo ‘<pre>’; 
 print_r($out) ; 
 echo ‘</pre>’;
?>

输出的结果:

 Array 
 ( 
     [getGeoIPContextResult] => Array 
         ( 
             [string] => Array 
                 ( 
                     [0] => 211.103.248.103 
                     [1] => 北京市昌平区 电信通 
                 ) 
         ) 
 ) 
 Array 
 ( 
     [getCountryCityByIpResult] => Array 
        ( 
             [string] => Array 
                 ( 
                     [0] => 95.173.45.28 
                     [1] => 美国 
                 ) 
        )    
 )
{jcomments on}{jcomments off}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注