首页 技术 正文
技术 2022年11月9日
0 收藏 664 点赞 4,748 浏览 1354 个字

实例代码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 ( “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 ‘<pre>’;
var_dump($client->__getFunctions());//获取服务器上提供的方法
echo “<hr>”;

echo ‘SOAP服务器提供的Type:’;
print_r($client->__getTypes());//获取服务器上数据类型
echo “<hr>”;

echo ‘执行GetGUIDNode的结果:’;
//查询中国北京的天气,返回的是一个结构体
$result=$client->getWeather(array(‘CityName’=>’beijing’,’CountryName’=>’china’));
echo $result->GetWeatherResult;//显示结果

?>

运行结果:PHP   调用web service接口(.net开发的接口)

对try和catch进行实例说明

eg:

<?php

//创建可抛出一个异常的函数
function checkNum($number) {
if($number>1) {
throw new Exception(“Value must be 1 or below”);
}
return true;
}

//在 “try” 代码块中触发异常
try {
//If the exception is thrown, this text will not be shown echo ‘If you see this, the number is 1 or below’;
checkNum(2);

}catch(Exception $e){
//捕获异常
echo ‘Message: ‘ .$e->getMessage();
}

?>

上面代码将获得类似这样一个错误:

Message: Value must be 1 or below 

例子解释:

上面的代码抛出了一个异常,并捕获了它:

  1. 创建 checkNum() 函数。它检测数字是否大于 1。如果是,则抛出一个异常。
  2. 在 “try” 代码块中调用 checkNum() 函数。
  3. checkNum() 函数中的异常被抛出
  4. “catch” 代码块接收到该异常,并创建一个包含异常信息的对象 ($e)。
  5. 通过从这个 exception 对象调用 $e->getMessage(),输出来自该异常的错误消息

不过,为了遵循“每个 throw 必须对应一个 catch”的原则,可以设置一个顶层的异常处理器来处理漏掉的错误。

上一篇: 19_ThreadLocal
下一篇: Dapper批量添加
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,906
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727