WordPress教程

使用WordPress函数向远程api发出Get和Post请求

阿里云

有时您可能希望向远程/外部 Api 发出请求以获取一些数据。也许您想在博客上显示最新的微博内容,或者您想从其他 WordPress 网站获取最新的文章。对于这些情况,WordPress 具有 wp_remote_get 和 wp_remote_post 函数。

发出获取(Get)请求

  1. <?php
  2. /**
  3.  * do_remote_get.
  4.  *
  5.  * Make a get request to a remote api,
  6.  *
  7.  * @see https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/
  8.  *
  9.  * @uses wp_remote_get() https://developer.wordpress.org/reference/functions/wp_remote_get/
  10.  * @uses json_decode() https://www.php.net/manual/en/function.json-decode.php
  11.  * @uses wp_remote_retrieve_body() https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/
  12.  * 
  13.  * @param String $url The url/endpoint to call
  14.  * @return Array
  15.  */
  16. function do_remote_get(String $url)
  17. {
  18.     $response = wp_remote_get($url, array(
  19.         'httpversion' => '1.1',
  20.         'blocking' => true
  21.     ));
  22.  
  23.     return json_decode(wp_remote_retrieve_body($response)) ?: [];
  24. }
也想出现在这里?联系我们
创客主机

在此代码段中,我们创建一个名为 do_remote_get 的新函数,该函数除了一个名为$url 的参数外,该参数必须为 string 类型。在我们的新函数中,我们使用 wp_remote_get 函数发出实际的 http 请求。wp_remote_get 函数接受两个参数:

  • $url(String):要调用的远程 URL /端点。在这种情况下,我们将传递给do_remote_get函数的$url变量传递给它。
  • $args(Array):请求的参数数组。这个数组可以有很多参数,但是在我们的例子中,我们只使用了两个。要使用的httpversion,并且我们将blocking设置为true,这意味着调用代码需要请求的结果。

请求完成后,我们将$response 传递给名为 wp_remote_retrieve_body 的函数。此函数检查响应是不是 WP_Error 对象,并具有有效的“ Body ”。如果这样做,它将返回响应主体 Body 。如果不是,它将返回一个空字符串。

然后,我们将输出传递给 json_decode 函数以解码返回的 Json 数据。现在请记住,wp_remote_retrieve_body 函数的返回值可以是一个空字符串,从而使 json_decode 返回一个伪造的值。这就是为什么我们在最后使用三元运算符 ?:[]来确保始终返回数组的原因。

现在,我们可以向 Api 发出 get 请求,如下所示:

  1. <?php
  2. $posts = do_remote_get('https://jsonplaceholder.typicode.com/posts/');
  3.  
  4. foreach ($posts as $post) {
  5.     echo "<h2>{$post->title}</h2>";
  6. }

在此示例中,我们使用新的 do_remote_get 函数向 JSONPlaceholder Api 发出 Get 请求,并获取一些(假)文章。然后,我们遍历文章并回显其标题。

注意:在此示例中,我们从 do_remote_get 函数中获取了对象数组。如果您希望将对象作为关联数组,则可以将 true 作为 seccond 参数传递给 json_decode 函数。

  1. <?php return json_decode(wp_remote_retrieve_body($response), true) ?: []; ?>

发出提交(Post)请求

在上面的示例中,我们使用 wp_remote_get 从远程 Api 获取一些文章。接下来,我们将处理提交请求,以在远程 Api 上创建文章。

  1. <?php
  2. /**
  3.  * do_remote_post.
  4.  *
  5.  * Make a post request to a remote api,
  6.  *
  7.  * @see https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/
  8.  *
  9.  * @uses wp_remote_post() https://developer.wordpress.org/reference/functions/wp_remote_post/
  10.  * @uses json_decode() https://www.php.net/manual/en/function.json-decode.php
  11.  * @uses wp_remote_retrieve_body() https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/
  12.  * 
  13.  * @param String $url The url/endpoint to call
  14.  * @param Array $data The data to send
  15.  * @return Array
  16.  */
  17. function do_remote_post(String $url, Array $data = [])
  18. {
  19.     $response = wp_remote_post($url, array(
  20.         'httpversion' => '1.1',
  21.         'blocking' => true,
  22.         'body' => $data
  23.     ));
  24.  
  25.     return json_decode(wp_remote_retrieve_body($response)) ?: [];
  26. }

对于 Post 请求,我们创建一个名为 do_remote_post 的新函数,该函数与 do_remote_get 函数相似,但是第二个参数$data 保留要发送到远程 Api 的数据。

在 do_remote_post 函数中,我们现在使用 wp_remote_post 函数发出请求。wp_remote_post 函数接受相同的参数,和 wp_remote_get 一一对应。对于 arguments 数组,我们传递了一个额外的参数 body ,并将$data 数组变量传递给了它。

现在,我们可以发出提交请求,以在 Api 上创建一个新文章,如下所示:

  1. <?php
  2. $response = do_remote_post('https://jsonplaceholder.typicode.com/posts/', [
  3.     'userId' => 1,
  4.     'title' => 'foo',
  5.     'body' => 'bar'
  6. ]);
  7.  
  8. var_dump($response);

在这里,我们使用 do_remote_post 函数向 JSONPlaceholder Api 发出发布请求,并向其传递网址/端点和代表我们要创建的发布的数组。

最后,我们使用 var_dump 打印来自 Api 的响应。JSONPlaceholder Api 将仅返回我们创建的文章的 Json 对象。

注意: Api 请求需要花费一些时间才能解决,因此最好将其缓存以加快页面加载速度。建议使用 WordPress 瞬态来缓存 Api 请求结果 。

以上内容出自: https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/ ,由 WordPress 大学 翻译整理。

使用 WordPress 函数向远程 api 发出 Get 和 Post 请求

已有 246 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!