当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Request_Soap - 类別

优质
小牛编辑
121浏览
2023-12-01

Request_Curl 类别主要是用来透过 PHP 的 SoapClient 扩充执行 SOAP 请求。 你的 PHP 安装必须以 "--enable-soap" 编译。你可以检查 phpinfo() 的输出来了解是否是此情况。

建立一个实例

你可以透过 Request 类别锻造一个此类别的实例:

// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 请注意,这只建立物件,不会执行请求!

set_params($params)

set_params 方法能让你在 SOAP 请求呼叫时设定传递参数。

静态
参数
参数 预设 描述
$params 必要 给请求的参数阵列。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 设定一些参数
$soap->set_params(array('userid' => 12, 'data' => $payload));

请注意,你必须定义参数的格式取决于被呼叫的 SOAP 服务。 例如,某些情况下期望它们被包裹在一个阵列中。 SOAP Request 驱动将以你在 set_params 使用的格式传递那些参数到该服务,所以确保那是正确的。

set_option($option, $value)

set_option 方法能让你定义一个 SoapClient 选项以被传递到该请求。

静态
参数
参数 预设 描述
$option 必要 要设定的 SoapClient 选项。
$value 必要 要设给此选项的值。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 设定用于该请求的使用者名称
$soap->set_option('login', $username);

set_options(array $options)

set_options 方法能让你定义多个 SoapClient 选项以被传递到该请求。

静态
参数
参数 预设 描述
$options 必要 要设定的 SoapClient 选项和值的阵列。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 设定用于该请求的使用者名称和密码
$soap->set_options(array(
	'login' => $username,
	'password' => $password,
	)
);

add_param($param, $value = null)

add_param 方法能让你添加一个或更多参数到已定义的。

静态
参数
参数 预设 描述
$param 必要 要设定的参数名称,或一个参数和值的阵列。
$value
null
要被定义的值。只用于当 $param 是一个字串值。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 添加一些参数
$soap->add_param(array('userid' => 12, 'data' => $payload));

// 或添加单一参数
$soap->add_param('data', $payload);

set_header($header, $content = null)

set_header 方法能让你设定一个 HTTP 请求表头做为该请求的一部分。

静态
参数
参数 预设 描述
$header 必要 HTTP 表头条目的名称。
$content
null
要被设定的表头值。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 传递一个认证符记到后端伺服器
$soap->set_header('auth-token', 'WV4YaeV8QeWVVVOE');

如果你需要设定一个非 "Name: Value" 形式的表头,在 $header 传递值,并且不要传递任何内容。

get_headers()

get_headers 方法能让你取回所有目前已定义的 HTTP 请求表头。

静态
参数 无。
回传 阵列,所有设定的表头。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 传递一个认证符记到后端伺服器
$soap->set_header('auth-token', 'WV4YaeV8QeWVVVOE');

// 回传 array('auth-token:WV4YaeV8QeWVVVOE')
$headers = $soap->get_headers();

set_auto_format($auto_format)

set_auto_format 方法能让你切换自动格式化开关。 Since 1.7.2, this is switched of by default, and when switched off, you will have to parse the cURL response yourself.

静态
参数
参数 预设 描述
$auto_format 必要 True 如果你希望回传的回应被转换为一个阵列,false 如果你想要存取它所接收到的。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 我们希望回传的是一个阵列
$soap->set_auto_format(true);

Auto formatting has support for the following mime types:

  • application/xml
  • text/xml
  • application/json
  • text/json
  • text/csv
  • application/csv
  • application/vnd.php.serialized
so when enabled and the response is in one of these types, it will be processed automatically, and returned to your controller in the form of a PHP array.

Only enable this is the source of the data is trustworthy, and/or if you have validated the received input. JSON and serialized arrays could contain objects. Since their constructor will execute upon instantiation during auto formatting, it may lead to unintended code execution, possibly compromizing your server!

execute(array $additional_params = array())

execute 方法执行定义的 SOAP 请求。

静态
参数
参数 预设 描述
$additional_params
array()
任何你想要传递给该请求的附加参数。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 执行该请求
$result = $soap->execute();

set_response($body, $status, $mime = null, $headers = array())

set_response 方法设定来自请求中接收的回应。

静态
参数
参数 预设 描述
$body 必要 要被设定做为回应主体的资料。
$status 必要 建立回应的 HTTP 状态。
$mime
null
该资料的 mime 类型。这是用在 auto_format 以转换该主体为一个阵列。
$headers
null
任何你想设定的 HTTP 回应表头。
回传 Response,建立的回应物件。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 建立一个自订的回应(SOAP 回应始终应为 XML)
$soap->set_response($body, $this->response_info('http_code', 200), 'xml', array('auth-token' => 'WV4YaeV8QeWVVVOE'));

一般情况下,你不应该使用这个方法。它是在请求执行之后用来预处理请求回应物件。

response()

response 方法回传目前请求的回应。

静态
参数 无。
回传 Response,建立的回应物件。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 假设一些参数和选项已经被设定,并且执行
$soap->execute();

// 取回 Response 物件的结果
$result = $soap->response();

response_info($key = null, $default = null)

response_info 方法能让你取回一个 SOAP 回应值,或所有回应值。

静态
参数
参数 预设 描述
$key
null
要取回的特定回应值。当你想要取回所有值时不要指定。
$default
null
当该请求值不存在时要被回传的值。
回传 混合,取决于请求值的资料类型。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 假设一些参数和选项已经被设定,并且执行
$soap->execute();

// 从回应表头取得执行的 soap 动作
$size = $soap->response_info('SOAPAction', false);

此方法允许存取该回应的 HTTP 表头。

set_function($function)

set_function 方法设定要在 SoapClient 执行的函式。

静态
参数
参数 预设 描述
$function 必要 要呼叫的 SOAP 函式名称。
回传 Request_Soap,鍊结用
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 设定 soapcall 函式以取回一些使用者资料
$soap->set_function('GetUserInfo');

get_functions()

get_functions 方法回传定义在 WSDL 中的函式列表。

静态
参数 无。
回传 可用的 SOAP 函式阵列
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 取回 SOAP 函式列表
$functions = $soap->get_functions();

这个方法需要 SoapClient 以在 WDSL 模式下运作。

get_request_xml()

get_request_xml 方法回传上一次请求的 XML。

静态
参数 无。
回传 字串,上一次的 XML 请求
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 一些程式码……

// 取回上一次的 XML 请求。
$request = $soap->get_request_xml();

这个方法需要启用 'trace' 选项。

get_request_headers()

get_request_headers 方法回传上一次请求的表头。

静态
参数 无。
回传 字串,来自上一次请求的 SOAP 表头。可能包含新行。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 一些程式码……

// 取回上一次请求的表头
$headers = $soap->get_request_headers();

这个方法需要启用 'trace' 选项。

get_response_xml()

get_response_xml 方法回传上一次回应的 XML。

静态
参数 无。
回传 字串,上一次的 XML 回应
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 一些程式码……

// 取回上一次的 XML 回应
$response = $soap->get_response_xml();

这个方法需要启用 'trace' 选项。

get_response_headers()

get_response_headers 方法回传上一次回应的表头。

静态
参数 无。
回传 字串,来自上一次回应的 SOAP 表头。可能包含新行。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 一些程式码……

// 取回上一次回应的表头
$response = $soap->get_response_headers();

这个方法需要启用 'trace' 选项。

get_types()

get_types 方法回传支援类型的列表。

静态
参数 无。
回传 SOAP 类型的阵列,详细描述所有结构和类型。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 取得对于此 web 服务的 WSDL 中所描述的类型列表
$types = $soap->get_types();

这个方法需要 SoapClient 以在 WDSL 模式下运作。

set_cookie($name, $value = null)

set_cookie 方法为后续请求设定请求 cookie。

静态
参数
参数 预设 描述
$name 必要 cookie 名称。
$value
null
cookie 值。如果没有指定,该 cookie 将被删除。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 在 HTTP 请求表头中设定一个认证符记做为一个 cookie
$soap->set_cookie('auth-token', 'WV4YaeV8QeWVVVOE');

set_location($location)

set_location 方法能让你更改端点位置。

静态
参数
参数 预设 描述
$location 必要 新端点的 URL。
範例
// 建立一个 Request_Soap 物件
$soap = Request::forge('http://soap.example.org/api/v1/this/info', 'soap');

// 更改请求的端点
$soap->set_location('http://moresoap.example.org/api/v2/other/info');