从外部服务获取数据

通过WordPress中的wp_remote_get()功能。此函数接受以下两个参数:

  1. $网址–从中检索数据的资源。这必须是标准的HTTP格式
  2. $个参数–可选–您可以在此处传递参数数组以更改行为和标头,例如cookie、遵循重定向等。

虽然可以通过$个参数参数:

  • 方法–GET
  • timeout–5–放弃前等待的时间
  • 重定向–5–遵循重定向的次数。
  • httpversion–1.0版
  • blocking–true–页面的其余部分是否应该等待完成加载,直到此操作完成?
  • 标题–数组()
  • 正文–空
  • cookie–数组()

因为github提供了一个优秀的API,对于许多公共方面不需要应用程序注册,我们将在以下示例中以GitHub API为目标。

让我们使用GitHub WordPress组织的URL,看看可以获得什么样的信息。

$response=wp_remote_get('https://api.github.com/users/wordpress网站' );

$响应将包含有关我们请求的所有标题、内容和其他元数据。

上一个示例的响应如下

阵列([headers]=>数组([服务器]=>nginx[日期]=>2012年10月5日星期五04:43:50 GMT[content-type]=>application/json;字符集=utf-8[连接]=>关闭[状态]=>200 OK[变化]=>接受[x-ratelimit-remaining]=>4988[内容长度]=>594[最后修改]=>2012年10月5日星期五04:39:58 GMT[etag]=>“5d5e6f7a09462d6a2b473fb616a26d2a”[x-github-media-type]=>github.beta[缓存控制]=>公共,s-maxage=60,max-age=60[x-content-type-options]=>个[x-ratelimit-limit]=>5000)[正文]=>{“登录”:“WordPress”,“id”:276006,“node_id”:“MDEyOk9yZ2FuaXphdGlvbjI3NjAwNg==”,“化身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/追随者",“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",“事件url”:“https://api.github.com/users/WordPress/events网站{/隐私}“,“received_events_url”:“https://api.github.com/users/WordPress/received_events网站",“type”:“组织”,“site_admin”:假,“name”:空,“company”:空,“博客”:“网址:https://wordpress.org/",“location”:空,“email”:空,“可雇佣”:无效,“bio”:空,“twitter_username”:空,“public_repos”:50,“public_gists”:0,“关注者”:0,“关注”:0,“created_at”:“2010-05-13T22:42:10Z”,“updated_at”:“2020-05-22T14:27:02Z”}[响应]=>阵列([保留的文本5237511b45884ac6db1ff9d7e407f225/]=>200[消息]=>确定)[cookies]=>数组()[文件名]=>)

得到你一直想要的身体

要检索响应正文,请使用wp_remote_retrieve_body()功能。此函数只接受一个参数,即来自wp_remote_get()功能。

$response=wp_remote_get('https://api.github.com/users/wordpress网站' );$body=wp_remote_retrieve_body($response);

使用$响应根据上一个示例,$主体将类似于:

{“登录”:“WordPress”,“id”:276006,“node_id”:“MDEyOk9yZ2FuaXphdGlvbjI3NjAwNg==”,“化身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",“事件url”:“https://api.github.com/users/WordPress/events网站{/隐私}“,“received_events_url”:“https://api.github.com/users/WordPress/received_events网站",“type”:“组织”,“site_admin”:假,“name”:空,“company”:空,“博客”:“https://wordpress.org/",“location”:空,“email”:空,“可雇佣”:空,“bio”:空,“twitter_username”:空,“public_repos”:50,“public_gists”:0,“关注者”:0,“关注”: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_代码将包含200。否则,它将包含一些HTTP状态代码。

获取特定标题

如果您希望检索特定的标头,例如上次修改的标头,您可以使用wp_remote_retrieve_header()。此函数采用两个参数

  1. $响应–get调用的响应
  2. $标题–要检索的标题的名称

要检索上次修改的页眉,请执行以下操作:

$last_modified=wp_remote_retrieve_header($response,'last-modified');

您还可以使用wp_remote_retrieve_headers()功能。

使用基本身份验证获取

受更多保护的API提供一种或多种不同类型的身份验证。一种常见的身份验证方法是HTTP Basic authentication,虽然不是高度安全的。它可以在WordPress中绕过“Authorization”到wp_remote_get()函数,以及其他HTTP方法函数。

$args=阵列(“headers”=>数组(“授权”=>“基本”。base64_encode(YOUR_USERNAME.':'.YOUR_PASSWORD)));wp_remote_get($url,$args);

更多关于AUTH的信息