通过 wp_remote_get() 函数,在 WordPress 中获取数据变得极其简单。此函数接受以下两个参数:
$url– 要从中检索数据的资源。必须采用标准 HTTP 格式$args– 可选 – 您可以在此处传递一个参数数组以修改行为和请求头,例如 cookie、跟随重定向等。
假设以下默认值,但可以通过 $args 参数进行更改:
- method – GET
- timeout – 5 – 等待多久后放弃
- redirection – 5 – 跟随重定向的次数。
- httpversion – 1.0
- blocking – true – 页面的其余部分是否应等待直到此操作完成?
- headers – array()
- body – null
- cookies – array()
因为 GitHub 提供了一个出色的 API,对于许多公开方面不需要注册应用,我们将在以下示例中针对 GitHub API。
让我们使用 GitHub WordPress 组织的 URL 来看看我们可以获取什么样的信息。
$response = wp_remote_get( 'https://api.github.com/users/wordpress' );$response 将包含有关我们请求的所有请求头、内容和元数据。
上一个示例的响应可能如下所示:
Array(
[headers] => Array(
[server] => nginx
[date] => Fri, 05 Oct 2012 04:43:50 GMT
[content-type] => application/json; charset=utf-8
[connection] => close
[status] => 200 OK
[vary] => Accept
[x-ratelimit-remaining] => 4988
[content-length] => 594
[last-modified] => Fri, 05 Oct 2012 04:39:58 GMT
[etag] => "5d5e6f7a09462d6a2b473fb616a26d2a"
[x-github-media-type] => github.beta
[cache-control] => public, s-maxage=60, max-age=60
[x-content-type-options] => nosniff
[x-ratelimit-limit] => 5000
)
[body] => {
"login": "WordPress",
"id": 276006,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjI3NjAwNg==",
"avatar_url": "https://avatars0.githubusercontent.com/u/276006?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/WordPress",
"html_url": "https://github.com/WordPress",
"followers_url": "https://api.github.com/users/WordPress/followers",
"following_url": "https://api.github.com/users/WordPress/following{/other_user}",
"gists_url": "https://api.github.com/users/WordPress/gists{/gist_id}",
"starred_url": "https://api.github.com/users/WordPress/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/WordPress/subscriptions",
"organizations_url": "https://api.github.com/users/WordPress/orgs",
"repos_url": "https://api.github.com/users/WordPress/repos",
"events_url": "https://api.github.com/users/WordPress/events{/privacy}",
"received_events_url": "https://api.github.com/users/WordPress/received_events",
"type": "Organization",
"site_admin": false,
"name": null,
"company": null,
"blog": "https://wordpress.org/",
"location": null,
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 50,
"public_gists": 0,
"followers": 0,
"following": 0,
"created_at": "2010-05-13T22:42:10Z",
"updated_at": "2020-05-22T14:27:02Z"
}
[response] => Array(
[preserved_text 5237511b45884ac6db1ff9d7e407f225 /] => 200
[message] => OK
)
[cookies] => Array()
[filename] =>
)获取您一直想要的正文
要检索响应正文,请使用 wp_remote_retrieve_body() 函数。此函数仅接受一个参数,即来自 wp_remote_get() 函数的响应。
$response = wp_remote_get( 'https://api.github.com/users/wordpress' );
$body = wp_remote_retrieve_body( $response );使用上一个示例中的 $response,$body 可能如下所示:
{
"login": "WordPress",
"id": 276006,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjI3NjAwNg==",
"avatar_url": "https://avatars0.githubusercontent.com/u/276006?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/WordPress",
"html_url": "https://github.com/WordPress",
"followers_url": "https://api.github.com/users/WordPress/followers",
"following_url": "https://api.github.com/users/WordPress/following{/other_user}",
"gists_url": "https://api.github.com/users/WordPress/gists{/gist_id}",
"starred_url": "https://api.github.com/users/WordPress/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/WordPress/subscriptions",
"organizations_url": "https://api.github.com/users/WordPress/orgs",
"repos_url": "https://api.github.com/users/WordPress/repos",
"events_url": "https://api.github.com/users/WordPress/events{/privacy}",
"received_events_url": "https://api.github.com/users/WordPress/received_events",
"type": "Organization",
"site_admin": false,
"name": null,
"company": null,
"blog": "https://wordpress.org/",
"location": null,
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 50,
"public_gists": 0,
"followers": 0,
"following": 0,
"created_at": "2010-05-13T22:42:10Z",
"updated_at": "2020-05-22T14:27:02Z"
}获取响应代码
您可能需要检查响应代码以确保检索成功。这可以通过 wp_remote_retrieve_response_code() 函数完成:
$http_code = wp_remote_retrieve_response_code( $response );如果成功,$http_code 将包含 200。否则,它将包含一些 HTTP 状态码。
获取特定的请求头
如果您希望检索特定的请求头,例如 last-modified,您可以使用 wp_remote_retrieve_header() 来实现。此函数接受两个参数:
$response– 来自 get 调用的响应$header– 要检索的请求头名称
要检索 last-modified 请求头:
$last_modified = wp_remote_retrieve_header( $response, 'last-modified' );您还可以使用 wp_remote_retrieve_headers() 函数以数组形式检索所有请求头。
使用基本认证进行 GET
受到更多保护的 API 提供多种不同类型的认证方式之一。一种常见(尽管安全性不高)的认证方法是 HTTP Basic Authentication。它可以在 WordPress 中使用,通过将“Authorization”传递给 wp_remote_get() 函数的第二个参数,以及其他 HTTP 方法函数。
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( YOUR_USERNAME . ':' . YOUR_PASSWORD )
)
);
wp_remote_get( $url, $args );关于认证的更多信息