dengwen3 发表于 2017-3-31 08:38:11

Hprose for PHP 客户端(二)

异常处理
Hprose for Hprose的客户端只支持同步调用,因此在调用过程中,如果服务器端发生错误,异常将在客户端被直接抛出,使用try...catch语句块即可捕获异常,通常服务器端调用返回的异常是HproseException类型。但是在调用过程中也可能抛出其它类型的异常。
例如,当调用不存在的方法时:
<?php
include("hprose/hproseHttpClient.php");
$client = new HproseHttpClient("http://www.hprose.com/example/");
echo "<pre>";
try {
echo $client->unexistMethod();
}
catch (Exception $e) {
print_r($e);
}
echo "</pre>";
?>
运行结果如下:
引用
HproseException Object
(
    => Can't find this function unexistmethod().
file: D:\phpox\hprose\www\example\hproseHttpServer.php
line: 152
trace: #0 D:\phpox\hprose\www\example\hproseHttpServer.php(374): HproseHttpServer->doInvoke()
#1 D:\phpox\hprose\www\example\hproseHttpServer.php(391): HproseHttpServer->handle()
#2 D:\phpox\hprose\www\example\index.php(58): HproseHttpServer->start()
#3 {main}
    =>
    => 0
    => /var/www/hprose/hproseHttpClient.php
    => 165
    => Array
      (
             => Array
                (
                   => /var/www/hprose/hproseHttpClient.php
                   => 191
                   => invoke
                   => HproseHttpClient
                   => ->
                   => Array
                        (
                            => unexistMethod
                            => Array
                              (
                              )
                        )
                )
             => Array
                (
                   => __call
                   => HproseHttpClient
                   => ->
                   => Array
                        (
                            => unexistMethod
                            => Array
                              (
                              )
                        )
                )
             => Array
                (
                   => /var/www/exam4.php
                   => 6
                   => unexistMethod
                   => HproseHttpClient
                   => ->
                   => Array
                        (
                        )
                )
      )
)

HTTP参数设置
目前的版本只提供了http客户端实现,针对于http客户端,有一些特别的设置,例如代理服务器、持久连接、http标头等设置,下面我们来分别介绍。
代理服务器
默认情况下,代理服务器是被禁用的。可以通过setProxy来设置http代理服务器的地址和端口。参数是字符串,例如:"tcp://10.54.1.39:8000",默认值为NULL。
HTTP标头
有时候您可能需要设置特殊的http标头,例如当您的服务器需要Basic认证的时候,您就需要提供一个Authorization标头。设置标头很简单,只需要调用setHeader方法就可以啦,该方法的第一个参数为标头名,第二个参数为标头值,这两个参数都是字符串型。如果将第二个参数设置为NULL,则表示删除这个标头。
标头名不可以为以下值:

[*]Context-Type
[*]Context-Length

因为这些标头有特别意义,客户端会自动设定这些值。
另外,Cookie这个标头不要轻易去设置它,因为设置它会影响Cookie的自动处理,如果您的通讯中用到了Session,通过setHeader方法来设置Cookie标头,将会影响Session的正常工作。
页: [1]
查看完整版本: Hprose for PHP 客户端(二)