Book/oauth-Phpdoc专题
OAuth
目录
- 简介
- 安装/配置
- 预定义常量
- 范例
- OAuth 函数
- oauth_get_sbs — 生成一个签名字符基串
- oauth_urlencode — 将 URI 编码为 RFC 3986 规范
- OAuth — OAuth 类
- OAuth::__construct — 新建一个 OAuth 对象
- OAuth::__destruct — 析构函数
- OAuth::disableDebug — 关闭详细的调试
- OAuth::disableRedirects — 关闭重定向
- OAuth::disableSSLChecks — 关闭 SSL 检查
- OAuth::enableDebug — 启用详细调试
- OAuth::enableRedirects — 启用重定向
- OAuth::enableSSLChecks — 启用 SSL 检查
- OAuth::fetch — 获取一个 OAuth 受保护的资源
- OAuth::generateSignature — 生成一个签名
- OAuth::getAccessToken — 获取一个访问令牌
- OAuth::getCAPath — 获取 CA 信息
- OAuth::getLastResponse — 获取最后一次的响应
- OAuth::getLastResponseHeaders — 获取最后一次响应的头信息
- OAuth::getLastResponseInfo — 获取关于最后一次响应的 HTTP 信息
- OAuth::getRequestHeader — 生成 OAuth 头信息字符串签名
- OAuth::getRequestToken — 获取一个请求令牌
- OAuth::setAuthType — 设置授权类型
- OAuth::setCAPath — 设置 CA 路径和信息
- OAuth::setNonce — 为后续请求设置现时标志
- OAuth::setRequestEngine — 设置目标请求引擎
- OAuth::setRSACertificate — 设置 RSA 证书
- OAuth::setSSLChecks — 调整特定的SSL请求检查
- OAuth::setTimestamp — 设置时间戳
- OAuth::setToken — 设置令牌和 secret
- OAuth::setVersion — 设置 OAuth 版本
- OAuthProvider — OAuthProvider 类
- OAuthProvider::addRequiredParameter — 添加必需的参数
- OAuthProvider::callconsumerHandler — 调用 consumerNonceHandler 回调函数
- OAuthProvider::callTimestampNonceHandler — 调用 timestampNonceHandler 回调函数
- OAuthProvider::calltokenHandler — 调用 tokenNonceHandler 回调函数
- OAuthProvider::checkOAuthRequest — 检查一个 oauth 请求
- OAuthProvider::__construct — 新建一个 OAuthProvider 对象
- OAuthProvider::consumerHandler — 设置 consumerHandler 句柄回调函数
- OAuthProvider::generateToken — 生成一个随机令牌
- OAuthProvider::is2LeggedEndpoint — is2LeggedEndpoint
- OAuthProvider::isRequestTokenEndpoint — 设置 isRequestTokenEndpoint
- OAuthProvider::removeRequiredParameter — 移除一个必需的参数
- OAuthProvider::reportProblem — 报告问题
- OAuthProvider::setParam — 设置一个参数
- OAuthProvider::setRequestTokenPath — 设置请求令牌路径
- OAuthProvider::timestampNonceHandler — 设置 timestampNonceHandler 句柄回调函数
- OAuthProvider::tokenHandler — 设置 tokenHandler 句柄回调函数
- OAuthException — OAuthException 类
简介
此 OAuth 扩展提供一个简单接口使用 OAuth HTTP 规范与数据提供者互动,以便保护私有资源。
类摘要
OAuth
class OAuth {
/* 属性 */
public $debug ;
public $sslChecks ;
public $debugInfo ;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">string
$consumer_key , <span
class="type">string $consumer_secret [, <span
class="methodparam">string
$signature_method =
OAUTH_SIG_METHOD_HMACSHA1 [, <span
class="methodparam">int $auth_type<span
class="initializer"> = 0 ]] )
public void __destruct ( <span class="methodparam">void )
public bool disableDebug ( <span class="methodparam">void )
public bool disableRedirects ( <span class="methodparam">void )
public bool disableSSLChecks ( <span class="methodparam">void )
public bool enableDebug ( <span class="methodparam">void )
public bool enableRedirects ( <span class="methodparam">void )
public bool enableSSLChecks ( <span class="methodparam">void )
public mixed
fetch ( <span
class="type">string $protected_resource_url [, <span
class="methodparam">array
$extra_parameters [, <span
class="type">string $http_method [, <span
class="methodparam">array
$http_headers ]]] )
public string
generateSignature ( <span
class="methodparam">string
$http_method , <span
class="type">string $url [, <span
class="methodparam">mixed
$extra_parameters ] )
public array
getAccessToken ( <span
class="methodparam">string
$access_token_url [, <span
class="type">string $auth_session_handle [, <span
class="methodparam">string
$verifier_token ]] )
public array getCAPath ( <span class="methodparam">void )
public string getLastResponse ( <span class="methodparam">void )
public string getLastResponseHeaders ( <span class="methodparam">void )
public array getLastResponseInfo ( <span class="methodparam">void )
public string
getRequestHeader ( <span
class="methodparam">string
$http_method , <span
class="type">string $url [, <span
class="methodparam">mixed
$extra_parameters ] )
public array
getRequestToken ( <span
class="methodparam">string
$request_token_url [, <span
class="type">string $callback_url ] )
public mixed
setAuthType ( <span
class="methodparam">int $auth_type )
public mixed
setCAPath ([ <span
class="methodparam">string $ca_path
[, string
$ca_info ]] )
public mixed
setNonce ( <span
class="methodparam">string $nonce )
public void
setRequestEngine ( <span
class="methodparam">int $reqengine )
public mixed
setRSACertificate ( <span
class="methodparam">string $cert )
public bool
setSSLChecks ( <span
class="methodparam">int $sslcheck )
public mixed
setTimestamp ( <span
class="methodparam">string $timestamp
)
public bool
setToken ( <span
class="methodparam">string $token ,
string
$token_secret )
public bool
setVersion ( <span
class="methodparam">string $version )
}
属性
debug
sslChecks
debugInfo
OAuth::__construct
新建一个 OAuth 对象
说明
public <span
class="methodname">OAuth::__construct ( <span
class="methodparam">string
$consumer_key , <span
class="type">string $consumer_secret [, <span
class="methodparam">string
$signature_method =
OAUTH_SIG_METHOD_HMACSHA1 [, <span
class="methodparam">int $auth_type<span
class="initializer"> = 0 ]] )
新建一个 OAuth 对象
参数
consumer_key
由服务提供者提供的 consumer key 。
consumer_secret
由服务提供者提供的 consumer secret 。
signature_method
可选参数,用来定义使用哪种签名方法,默认为
OAUTH_SIG_METHOD_HMACSHA1 (HMAC-SHA1)。
auth_type
可选参数,用来定义如何传递 OAuth
参数给消费方,默认为OAUTH_AUTH_TYPE_AUTHORIZATION (在
Authorization 头部)。
OAuth::__destruct
析构函数
说明
public void OAuth::__destruct ( <span class="methodparam">void )
析构函数。
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
没有返回值。
参见
- OAuth::__construct
OAuth::disableDebug
关闭详细的调试
说明
public bool OAuth::disableDebug ( <span class="methodparam">void )
关闭详细的请求信息(默认为关闭)。或者,将
调试 属性设置为
false 值来关闭调试。
参数
此函数没有参数。
返回值
true
更新日志
| 版本 | 说明 |
|---|---|
| 0.99.8 | 增加相关 调试 属性。 |
参见
- OAuth::enableDebug
OAuth::disableRedirects
关闭重定向
说明
public bool OAuth::disableRedirects ( <span class="methodparam">void )
禁用自动跟随其后的重定向,这样,手动重定向请求。
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
true
参见
- OAuth::enableRedirects
OAuth::disableSSLChecks
关闭 SSL 检查
说明
public bool OAuth::disableSSLChecks ( <span class="methodparam">void )
关闭通常的 SSL 对等证书和主机检查,但不用于生产环境。或者,设置
sslChecks 成员为 false 来关闭 SSL 检查。
参数
此函数没有参数。
返回值
true
更新日志
| 版本 | 说明 |
|---|---|
| 0.99.8 | 增加了 sslChecks 成员 |
参见
- OAuth::enableSSLChecks
OAuth::enableDebug
启用详细调试
说明
public bool OAuth::enableDebug ( <span class="methodparam">void )
打开用于调试的详细请求信息,调试信息存储在 debugInfo
成员中。或者,可以设置 debug 成员为一个非 false
值来打开启用调试。
参数
此函数没有参数。
返回值
true
更新日志
| 版本 | 说明 |
|---|---|
| 0.99.8 | 增加了 debug 和 debugInfo 成员 |
参见
- OAuth::disableDebug
OAuth::enableRedirects
启用重定向
说明
public bool OAuth::enableRedirects ( <span class="methodparam">void )
启用自动重定向,此项默认为启用。
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
true
参见
- OAuth::disableRedirects
OAuth::enableSSLChecks
启用 SSL 检查
说明
public bool OAuth::enableSSLChecks ( <span class="methodparam">void )
启用通常的l SSL 对等证书和主机检查(默认为启用)。或者,可以设置
sslChecks 成员为一个非 false 值来启用 SSL 检查。
参数
此函数没有参数。
返回值
true
更新日志
| 版本 | 说明 |
|---|---|
| 0.99.8 | 增加 sslChecks 成员 |
参见
- OAuth::disableSSLChecks
OAuth::fetch
获取一个 OAuth 受保护的资源
说明
public mixed
OAuth::fetch ( <span
class="methodparam">string
$protected_resource_url [, <span
class="type">array $extra_parameters [, <span
class="methodparam">string
$http_method [, <span
class="type">array $http_headers ]]] )
获取一个资源。
参数
protected_resource_url
OAuth 受保护资源的URL
extra_parameters
和资源请求一起发送的额外参数。
http_method
*`OAUTH_HTTPMETHOD`** 系列
OAUTH 常量之一,GET、POST、PUT、HEAD
或 DELETE 其中的一个。
HEAD (OAUTH_HTTP_METHOD_HEAD )可以用于先于请求发现信息(如果
OAuth 证书在 Authorization 头部)。
http_headers
HTTP 客户端头信息(像 User-Agent, Accept 等等这样的)。
返回值
成功时返回 true, 或者在失败时返回 false。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
| 0.99.5 | 新增 http_method 参数 |
| 0.99.8 | 新增 http_headers 参数 |
范例
示例 #1 OAuth::fetch example
<?php
try {
$oauth = new OAuth("consumer_key","consumer_secret",OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->setToken("access_token","access_token_secret");
$oauth->fetch("http://photos.example.net/photo?file=vacation.jpg");
$response_info = $oauth->getLastResponseInfo();
header("Content-Type: {$response_info["content_type"]}");
echo $oauth->getLastResponse();
} catch(OAuthException $E) {
echo "Exception caught!\n";
echo "Response: ". $E->lastResponse . "\n";
}
?>
参见
- OAuth::getLastResponse
- OAuth::getLastResponseInfo
- OAuth::setToken
OAuth::generateSignature
生成一个签名
说明
public string
OAuth::generateSignature ( <span
class="methodparam">string
$http_method , <span
class="type">string $url [, <span
class="methodparam">mixed
$extra_parameters ] )
生成一个基于最终 HTTP 方法、URL 和 一个字符串/数组参数的签名。
参数
http_method
用来请求的 HTTP 方法
url
用来请求的 URL
extra_parameters
字符串或数组的附加参数
返回值
一个包含签名的字符串 或者在失败时返回 false
OAuth::getAccessToken
获取一个访问令牌
说明
public array
OAuth::getAccessToken ( <span
class="methodparam">string
$access_token_url [, <span
class="type">string $auth_session_handle [, <span
class="methodparam">string
$verifier_token ]] )
从服务提供者获取一个访问令牌、secret以及一些附带的响应参数 。
参数
access_token_url
用于访问令牌 API 的 URL。
auth_session_handle
授权会话句柄,此参数在 OAuth 规范核心中没有任何引用,
但可能被大的提供者实现。» 参见 ScalableOAuth
获取更多信息。
verifier_token
对于支持 1.0a 的服务提供者, 当交换请求令牌和访问令牌时,必须传递
verifier_token 。如果 verifier_token 存在于 $_GET 或 $_POST
中,它将被自动传递,且调用者不需要指定一个 verifier_token
(通常如果访问令牌在 oauth_callback URL 上被交换 )。
» 参见 ScalableOAuth
获取更多信息。
返回值
成功则返回一个包含解析过的 OAuth 响应的数组, 失败则返回 false 。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
| 0.99.9 | 新增 verifier_token 参数 |
范例
示例 #1 OAuth::getAccessToken 例子
<?php
try {
$oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
$oauth->setToken($request_token,$request_token_secret);
$access_token_info = $oauth->getAccessToken("https://example.com/oauth/access_token");
if(!empty($access_token_info)) {
print_r($access_token_info);
} else {
print "Failed fetching access token, response was: " . $oauth->getLastResponse();
}
} catch(OAuthException $E) {
echo "Response: ". $E->lastResponse . "\n";
}
?>
以上例程的输出类似于:
Array
(
[oauth_token] => some_token
[oauth_token_secret] => some_token_secret
)
参见
- OAuth::getLastResponse
- OAuth::getLastResponseInfo
- OAuth::setToken
OAuth::getCAPath
获取 CA 信息
说明
public array OAuth::getCAPath ( <span class="methodparam">void )
获取证书授权的信息,其中包括通过 <span class="methodname">OAuth::setCaPath 设置的 ca_path 和 ca_info 。
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
返回一个证书授权信息的 数组 ,在返回的关联数组中明确地包含 ca_path 和 ca_info 键。
参见
- OAuth::setCAPath
- OAuth::getLastResponseInfo
OAuth::getLastResponse
获取最后一次的响应
说明
public string OAuth::getLastResponse ( <span class="methodparam">void )
获取最近一次请求的原始响应。
参数
此函数没有参数。
返回值
返回一个最后一次响应的字符串。
参见
- OAuth::getLastResponseInfo
- OAuth::fetch
OAuth::getLastResponseHeaders
获取最后一次响应的头信息
说明
public string OAuth::getLastResponseHeaders ( <span class="methodparam">void )
获取最后一次响应的头信息。
参数
此函数没有参数。
返回值
返回一个包含最后一次响应头信息的字符串 或者在失败时返回 false 。
OAuth::getLastResponseInfo
获取关于最后一次响应的 HTTP 信息
说明
public array OAuth::getLastResponseInfo ( <span class="methodparam">void )
获取关于最后一次响应的 HTTP 信息。
参数
此函数没有参数。
返回值
返回一个包含最后一次请求响应信息的数组。可以用到来自 <span class="function">curl_getinfo 的常量。
参见
- OAuth::fetch
- OAuth::getLastResponse
OAuth::getRequestHeader
生成 OAuth 头信息字符串签名
说明
public string
OAuth::getRequestHeader ( <span
class="methodparam">string
$http_method , <span
class="type">string $url [, <span
class="methodparam">mixed
$extra_parameters ] )
生成基于最终 HTTP 方法、URL 和 一个字符串/数组附加参数的 OAuth 头信息字符串签名。
参数
http_method
请求的 HTTP 方法。
url
请求的 URL 。
extra_parameters
字符串或数组类型的附带参数。
返回值
返回 一个包含生成的请求头信息的字符串 r 或者在失败时返回 false
OAuth::getRequestToken
获取一个请求令牌
说明
public array
OAuth::getRequestToken ( <span
class="methodparam">string
$request_token_url [, <span
class="type">string $callback_url ] )
从服务提供者那里获取一个请求令牌、secret 、以及一些附带的响应参数。
参数
request_token_url
请求令牌 API 的 URL。
callback_url
OAuth 回调 URL。 如果传递了 callback_url
且为空值,则将其设置为“oob”即到 OAuth 2009.1 咨询的地址。
返回值
成功则返回一个包含解析过了的 OAuth 响应的数组,失败则返回 false 。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
| 0.99.9 | 增加 callback_url 参数。 |
范例
示例 #1 OAuth::getRequestToken 例子
<?php
try {
$oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
$request_token_info = $oauth->getRequestToken("https://example.com/oauth/request_token");
if(!empty($request_token_info)) {
print_r($request_token_info);
} else {
print "Failed fetching request token, response was: " . $oauth->getLastResponse();
}
} catch(OAuthException $E) {
echo "Response: ". $E->lastResponse . "\n";
}
?>
以上例程的输出类似于:
Array
(
[oauth_token] => some_token
[oauth_token_secret] => some_token_secret
)
参见
- OAuth::getLastResponse
- OAuth::getLastResponseInfo
OAuth::setAuthType
设置授权类型
说明
public mixed
OAuth::setAuthType ( <span
class="methodparam">int $auth_type )
设置 OAuth 参数应该放在哪里传递。
参数
auth_type
auth_type可能是下列标志之一(在 OAuth 1.0 规范的第 5.2
章节中按优先级降序排列):
OAUTH_AUTH_TYPE_AUTHORIZATION
在 HTTP Authorization 头部传递 OAuth 参数。
OAUTH_AUTH_TYPE_FORM
将 OAuth 参数附加到 HTTP POST 请求主体中。
OAUTH_AUTH_TYPE_URI
将 OAuth 参数附加到请求的 URI 后面 。
OAUTH_AUTH_TYPE_NONE
无。
返回值
如果参数设置正确则返回 true ,否则返回 false
(比如,传递进一个无效的 auth_type )。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
OAuth::setCAPath
设置 CA 路径和信息
说明
public mixed
OAuth::setCAPath ([ <span
class="methodparam">string $ca_path
[, string
$ca_info ]] )
设置证书授权中心(CA )的路径和信息。
Warning
本函数还未编写文档,仅有参数列表。
参数
ca_path
要设置的 CA 路径。
ca_info
要设置的 CA 信息。
返回值
成功则返回 true ,如果 ca_path 或 ca_info
其中之一被认为无效则返回 false 。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
参见
- OAuth::getCaPath
OAuth::setNonce
为后续请求设置现时标志
说明
public mixed
OAuth::setNonce ( <span
class="methodparam">string $nonce )
为所有后续请求设置现时标志。
参数
nonce
oauth_nonce 的值。
返回值
成功返回 true ,如果 nonce 被认为无效,则返回 false 。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
参见
- OAuth::setToken
- OAuth::setAuthType
- OAuth::setVersion
OAuth::setRequestEngine
设置目标请求引擎
说明
public void
OAuth::setRequestEngine ( <span
class="methodparam">int $reqengine )
设置请求引擎,用于发送 HTTP 请求。
Warning
本函数还未编写文档,仅有参数列表。
参数
reqengine
想要的请求引擎。 设为 OAUTH_REQENGINE_STREAMS 则使用 PHP 流,设为
OAUTH_REQENGINE_CURL 则使用
Curl。
返回值
没有返回值。
错误/异常
如果选择了一个无效的请求引擎,则发出一个 <span class="classname">OAuthException 异常。
范例
示例 #1 OAuth::setRequestEngine 例子
<?php
$consumer = new OAuth();
$consumer->setRequestEngine(OAUTH_REQENGINE_STREAMS);
?>
参见
OAuth::setRSACertificate
设置 RSA 证书
说明
public mixed
OAuth::setRSACertificate ( <span
class="methodparam">string $cert )
设置 RSA 证书。
Warning
本函数还未编写文档,仅有参数列表。
参数
cert
RSA 证书。
返回值
成功则返回 true ,失败返回 false
(例如,RSA证书不能被传递)。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
范例
示例 #1 一个 OAuth::setRsaCertificate 例子
<?php
$consume = new OAuth('1234', '', OAUTH_SIG_METHOD_RSASHA1);
$consume->setRSACertificate(file_get_contents('test.pem'));
?>
参见
- OAuth::setCaPath
OAuth::setSSLChecks
调整特定的SSL请求检查
说明
public bool
OAuth::setSSLChecks ( <span
class="methodparam">int $sslcheck )
调整特定的SSL请求检查。
Warning
本函数还未编写文档,仅有参数列表。
参数
sslcheck
返回值
成功时返回 true, 或者在失败时返回 false。
OAuth::setTimestamp
设置时间戳
说明
public mixed
OAuth::setTimestamp ( <span
class="methodparam">string $timestamp
)
为后续请求设置时间戳。
Warning
本函数还未编写文档,仅有参数列表。
参数
timestamp
时间戳。
返回值
返回 true ,除非 timestamp 无效,则返回 false 。
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.0 | 以前失败时返回 null,而不是 false。 |
参见
- OAuth::setNonce
OAuth::setToken
设置令牌和 secret
说明
public bool
OAuth::setToken ( <span
class="methodparam">string $token ,
string
$token_secret )
为后续请求设置令牌和 secret。
参数
token
OAuth 令牌
token_secret
OAuth 令牌 secret。
返回值
true
范例
示例 #1 OAuth::setToken 例子
<?php
$oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
$oauth->setToken("token","token-secret");
?>
OAuth::setVersion
设置 OAuth 版本
说明
public bool
OAuth::setVersion ( <span
class="methodparam">string $version )
为随后请求设置 OAuth 版本
参数
version
OAuth 版本,默认值为 "1.0"
返回值
成功时返回 true, 或者在失败时返回 false。
简介
管理一个 OAuth 提供者类。
参考一个外部的深入教程 » 写一个 OAuth 提供者服务,用来亲自实践提供服务。也可以参考 OAuth 扩展源代码里的 » OAuth 提供者例子 。
类摘要
OAuthProvider
class OAuthProvider {
/* 方法 */
final public
bool <span
class="methodname">addRequiredParameter ( <span
class="methodparam">string
$req_params )
public void callconsumerHandler ( <span class="methodparam">void )
public void callTimestampNonceHandler ( <span class="methodparam">void )
public void calltokenHandler ( <span class="methodparam">void )
public void
checkOAuthRequest ([ <span
class="methodparam">string $uri [,
string
$method ]] )
public <span
class="methodname">__construct ([ <span
class="methodparam">array
$params_array ] )
public void
consumerHandler ( <span
class="methodparam">callable
$callback_function )
final public
static string
generateToken ( <span
class="methodparam">int $size [,
bool $strong<span
class="initializer"> = false ] )
public void
is2LeggedEndpoint ( <span
class="methodparam">mixed
$params_array )
public void
isRequestTokenEndpoint ( <span
class="methodparam">bool
$will_issue_request_token )
final public
bool <span
class="methodname">removeRequiredParameter ( <span
class="methodparam">string
$req_params )
final public
static string
reportProblem ( <span
class="methodparam">string
$oauthexception [, <span
class="type">bool $send_headers =
true ] )
final public
bool setParam
( string
$param_key [, <span
class="type">mixed $param_val ] )
final public
bool <span
class="methodname">setRequestTokenPath ( <span
class="methodparam">string $path )
public void
timestampNonceHandler ( <span
class="methodparam">callable
$callback_function )
public void
tokenHandler ( <span
class="methodparam">callable
$callback_function )
}
OAuthProvider::addRequiredParameter
添加必需的参数
说明
final public
bool <span
class="methodname">OAuthProvider::addRequiredParameter ( <span
class="methodparam">string
$req_params )
添加必需的 oauth 提供者参数。
Warning
本函数还未编写文档,仅有参数列表。
参数
req_params
必需的参数。
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- <span class="methodname">OAuthProvider::removeRequiredParameter
OAuthProvider::callconsumerHandler
调用 consumerNonceHandler 回调函数
说明
public void OAuthProvider::callconsumerHandler ( void )
调用注册的消费者句柄回调函数,此回调函数通过 <span class="methodname">OAuthProvider::consumerHandler 设置。
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
没有返回值。
错误/异常
如回调函数无法被调用或未被指定,会引发一个 E_ERROR 级别的错误。
参见
- OAuthProvider::consumerHandler
OAuthProvider::callTimestampNonceHandler
调用 timestampNonceHandler 回调函数
说明
public void OAuthProvider::callTimestampNonceHandler ( void )
调用注册的时间戳句柄回调函数,此回调函数通过 <span class="methodname">OAuthProvider::timestampNonceHandler 设置。
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
没有返回值。
错误/异常
如回调函数无法被调用或未被指定,会引发一个 E_ERROR 级别的错误。
参见
- OAuthProvider::timestampNonceHandler
OAuthProvider::calltokenHandler
调用 tokenNonceHandler 回调函数
说明
public void OAuthProvider::calltokenHandler ( <span class="methodparam">void )
调用注册的令牌句柄回调函数,此回调函数通过 <span class="methodname">OAuthProvider::tokenHandler 设置。
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
没有返回值。
错误/异常
如回调函数无法被调用或未被指定,会引发一个 E_ERROR 级别的错误。
参见
- OAuthProvider::tokenHandler
OAuthProvider::checkOAuthRequest
检查一个 oauth 请求
说明
public void
OAuthProvider::checkOAuthRequest ([
string $uri
[, string
$method ]] )
检查一个 OAuth 请求。
Warning
本函数还未编写文档,仅有参数列表。
参数
uri
可选的 URI 或终点。
method
HTTP 方法。 可选 *`OAUTH_HTTPMETHOD`**
OAuth 常量
其中之一传递。
返回值
没有返回值。
错误/异常
如果不能检测到 HTTP 方法,则发出一个 E_ERROR 级别的错误。
参见
- OAuthProvider::reportProblem
OAuthProvider::__construct
新建一个 OAuthProvider 对象
说明
public <span
class="methodname">OAuthProvider::__construct ([ <span
class="methodparam">array
$params_array ] )
发起一个新的 OAuthProvider <span class="type">对象。
Warning
本函数还未编写文档,仅有参数列表。
参数
params_array
设置限制 CLI SAPI
的可选参数。
返回值
返回一个 OAuthProvider <span class="type">对象。
范例
示例 #1 OAuthProvider::__construct 例子
<?php
try {
$op = new OAuthProvider();
// 使用用户定义的回调函数
$op->consumerHandler(array($this, 'lookupConsumer'));
$op->timestampNonceHandler(array($this, 'timestampNonceChecker'));
$op->tokenHandler(array($this, 'myTokenHandler'));
// 忽略 foo_uri 参数
$op->setParam('foo_uri', NULL);
// 对于终点不需要令牌
$op->setRequestTokenPath('/v1/oauth/request_token');
$op->checkOAuthRequest();
} catch (OAuthException $e) {
echo OAuthProvider::reportProblem($e);
}
?>
参见
- OAuthProvider::setParam
OAuthProvider::consumerHandler
设置 consumerHandler 句柄回调函数
说明
public void
OAuthProvider::consumerHandler ( <span
class="methodparam">callable
$callback_function )
设置消费者句柄回调函数,并将在后面通过 <span class="methodname">OAuthProvider::callConsumerHandler 被调用。
Warning
本函数还未编写文档,仅有参数列表。
参数
callback_function
回调类型 函数名。
返回值
没有返回值。
范例
示例 #1 <span class="methodname">OAuthProvider::consumerHandler 回调的例子
<?php
function lookupConsumer($provider) {
if ($provider->consumer_key === 'unknown') {
return OAUTH_CONSUMER_KEY_UNKNOWN;
} else if($provider->consumer_key == 'blacklisted' || $provider->consumer_key === 'throttled') {
return OAUTH_CONSUMER_KEY_REFUSED;
}
$provider->consumer_secret = "the_consumers_secret";
return OAUTH_OK;
}
?>
参见
- OAuthProvider::callConsumerHandler
OAuthProvider::generateToken
生成一个随机令牌
说明
final public
static string
OAuthProvider::generateToken ( <span
class="methodparam">int $size [,
bool $strong<span
class="initializer"> = false ] )
生成一个伪随机字节的 字符串 。
参数
size
想要的令牌长度,单位为字节。
strong
设置为 true 则意味着将对熵使用 /dev/random ,否则使用非阻塞的
/dev/urandom。在 Windows 平台将忽略此参数。
返回值
生成的令牌,一个以字节为单位的 字符串 。
错误/异常
如果 strong 参数为 true , 则当回退到用 <span
class="function">rand 来实现填充剩余的随机字节的时候,将触发一个
E_WARNING 级别的错误(比如,当最初找不到足够的随机数据的时候)。
范例
示例 #1 OAuthProvider::generateToken 例子
<?php
$p = new OAuthProvider();
$t = $p->generateToken(4);
echo strlen($t), PHP_EOL;
echo bin2hex($t), PHP_EOL;
?>
以上例程的输出类似于:
4
b6a82c27
注释
Note:
当系统没有足够的随机数据可用的时候,此函数将使用 PHP 内部的 <span class="function">rand 来实现填充剩余的随机字节。
参见
- openssl_random_pseudo_bytes
- mcrypt_create_iv
OAuthProvider::is2LeggedEndpoint
is2LeggedEndpoint
说明
public void
OAuthProvider::is2LeggedEndpoint ( <span
class="methodparam">mixed
$params_array )
2-legged 流程,或请求签名。不需要令牌。
Warning
本函数还未编写文档,仅有参数列表。
参数
params_array
返回值
返回一个 OAuthProvider <span class="type">对象.
范例
示例 #1 <span class="function">OAuthProvider::is2LeggedEndpoint 例子
<?php
$provider = new OAuthProvider();
$provider->is2LeggedEndpoint(true);
?>
参见
- OAuthProvider::__construct
OAuthProvider::isRequestTokenEndpoint
设置 isRequestTokenEndpoint
说明
public void
OAuthProvider::isRequestTokenEndpoint (
bool
$will_issue_request_token )
Warning
本函数还未编写文档,仅有参数列表。
参数
will_issue_request_token
设置是否发布一个请求令牌,从而决定 <span
class="methodname">OAuthProvider::tokenHandler 是否需要被调用。
返回值
没有返回值。
参见
- OAuthProvider::setRequestTokenPath
- OAuthProvider::reportProblem
OAuthProvider::removeRequiredParameter
移除一个必需的参数
说明
final public
bool <span
class="methodname">OAuthProvider::removeRequiredParameter ( <span
class="methodparam">string
$req_params )
移除一个必需的参数。
Warning
本函数还未编写文档,仅有参数列表。
参数
req_params
要被移除的必需参数。
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- OAuthProvider::setParam
- OAuthProvider::addRequiredParameter
OAuthProvider::reportProblem
报告问题
说明
final public
static string
OAuthProvider::reportProblem ( <span
class="methodparam">string
$oauthexception [, <span
class="type">bool $send_headers =
true ] )
将问题作为一个 OAuthException 异常传入,可能出现的问题在 OAuth 常量 章节里已列出。
Warning
本函数还未编写文档,仅有参数列表。
参数
oauthexception
OAuthException 类。
返回值
没有返回值。
参见
- OAuthProvider::checkOAuthRequest
- <span class="methodname">OAuthProvider::isRequestTokenEndpoint
OAuthProvider::setParam
设置一个参数
说明
final public
bool <span
class="methodname">OAuthProvider::setParam ( <span
class="methodparam">string $param_key
[, mixed
$param_val ] )
设置一个参数。
Warning
本函数还未编写文档,仅有参数列表。
参数
param_key
参数的 key 。
param_val
参数的值,此为可选项。
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- OAuthProvider::addRequiredParameter
OAuthProvider::setRequestTokenPath
设置请求令牌路径
说明
final public
bool <span
class="methodname">OAuthProvider::setRequestTokenPath ( <span
class="methodparam">string $path )
设置请求令牌路径。
Warning
本函数还未编写文档,仅有参数列表。
参数
path
路径。
返回值
true
参见
- OAuthProvider::tokenHandler
OAuthProvider::timestampNonceHandler
设置 timestampNonceHandler 句柄回调函数
说明
public void
OAuthProvider::timestampNonceHandler (
callable
$callback_function )
设置时间戳 nonce 句柄回调函数,此函数将在后面被 <span class="methodname">OAuthProvider::callTimestampNonceHandler 调用。跟时间戳/nonce相关的错误将被抛给此回调函数。
Warning
本函数还未编写文档,仅有参数列表。
参数
callback_function
回调类型 的函数名。
返回值
没有返回值。
范例
示例 #1 <span class="methodname">OAuthProvider::timestampNonceHandler 回调的例子
<?php
function timestampNonceChecker($provider) {
if ($provider->nonce === 'bad') {
return OAUTH_BAD_NONCE;
} elseif ($provider->timestamp == '0') {
return OAUTH_BAD_TIMESTAMP;
}
return OAUTH_OK;
}
?>
参见
- <span class="methodname">OAuthProvider::callTimestampNonceHandler
OAuthProvider::tokenHandler
设置 tokenHandler 句柄回调函数
说明
public void
OAuthProvider::tokenHandler ( <span
class="methodparam">callable
$callback_function )
设置令牌句柄的回调函数,此回调函数将在后面被 <span class="methodname">OAuthProvider::callTokenHandler 调用。
Warning
本函数还未编写文档,仅有参数列表。
参数
callback_function
回调类型 的函数名。
返回值
没有返回值。
范例
示例 #1 OAuthProvider::tokenHandler 回调的例子
<?php
function tokenHandler($provider) {
if ($provider->token === 'rejected') {
return OAUTH_TOKEN_REJECTED;
} elseif ($provider->token === 'revoked') {
return OAUTH_TOKEN_REVOKED;
}
$provider->token_secret = "the_tokens_secret";
return OAUTH_OK;
}
?>
参见
- OAuthProvider::callTokenHandler
简介
当使用 OAuth 扩展发生异常错误时,抛出此异常和包含了一些有用的调试信息。
类摘要
OAuthException
class OAuthException <span class="ooclass"> extends Exception {
/* 属性 */
public $lastResponse ;
public $debugInfo ;
/* 继承的属性 */
protected string
$message ;
protected int
$code ;
protected string
$file ;
protected int
$line ;
/* 继承的方法 */
final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )
public string Exception::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )
}
属性
lastResponse
发生异常时的响应,如果有的话
debugInfo