实例代码1: try { $this-soapClientObj = new SoapClient(self::URL . '?wsdl', array('connection_timeout' = self::CONNECTION_TIMEOUT)); } catch (Exception $e) { throw new Exception($e-getMessage(), $e-getCode()); } 实例代码2: ?php header ( "
实例代码1:
try {
$this->soapClientObj = new SoapClient(self::URL . '?wsdl', array('connection_timeout' => self::CONNECTION_TIMEOUT));
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
实例代码2:
header ( "Content-Type: text/html; charset=utf-8" );
/*
* 指定WebService路径并初始化一个WebService客户端
*/
$ws = "http://www.webservicex.net/globalweather.asmx?wsdl";//webservice服务的地址
$client = new SoapClient ($ws);
/*
* 获取SoapClient对象引用的服务所提供的所有方法
*/
echo 'SOAP服务器提供的开放函数:';
echo '
';<br />var_dump($client->__getFunctions());//获取服务器上提供的方法<br />echo "<hr>";</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p> <p><br />echo 'SOAP服务器提供的Type:';<br />print_r($client->__getTypes());//获取服务器上数据类型<br />echo "<hr>";</p> <p><br />echo '执行GetGUIDNode的结果:';<br />//查询中国北京的天气,返回的是一个结构体<br />$result=$client->getWeather(array('CityName'=>'beijing','CountryName'=>'china'));<br />echo $result->GetWeatherResult;//显示结果</p> <p>?></p> <p>运行结果:@@##@@</p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p>对try和catch进行实例说明</p> <p>eg:</p> <p><?php</p> <p>//创建可抛出一个异常的函数 <br />function checkNum($number) { <br /> if($number>1) { <br /> throw new Exception("Value must be 1 or below"); <br /> } <br /> return true; <br />} </p> <p><br />//在 "try" 代码块中触发异常 <br />try { <br /> //If the exception is thrown, this text will not be shown echo 'If you see this, the number is 1 or below'; <br /> checkNum(2); <br /> <br />}catch(Exception $e){ <br /> //捕获异常<br /> echo 'Message: ' .$e->getMessage(); <br />} </p> <p>?></p> <p>上面代码将获得类似这样一个错误:</p> <pre class="brush:php;toolbar:false">Message: Value must be 1 or below
上面的代码抛出了一个异常,并捕获了它:
不过,为了遵循“每个 throw 必须对应一个 catch”的原则,可以设置一个顶层的异常处理器来处理漏掉的错误。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号