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

2023-04-2807:41:42后端程序开发Comments1,112 views字数 3066阅读模式

可能希望向远程/外部Api发出请求以获取一些数据。也许您想在博客上显示最新的微博内容,或者您想从其他WordPress网站获取最新的文章。对于这些情况,WordPress具有wp_remote_getwp_remote_post 函数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

WordPress 函数向远程api发出Get和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. return json_decode(wp_remote_retrieve_body($response)) ?: [];
  23. }

在此代码段中,我们创建一个名为do_remote_get的新函数,该函数除了一个名为$url的参数外,该参数必须为string类型。在我们的新函数中,我们使用wp_remote_get函数发出实际的http请求。wp_remote_get函数接受两个参数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

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

请求完成后,我们将$response传递给名为wp_remote_retrieve_body的函数。此函数检查响应是不是WP_Error对象,并具有有效的“  Body  ”。如果这样做,它将返回响应主体 Body 。如果不是,它将返回一个空字符串。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

然后,我们将输出传递给json_decode函数以解码返回的Json数据。现在请记住,wp_remote_retrieve_body函数的返回值可以是一个空字符串,从而使json_decode返回一个伪造的值。这就是为什么我们在最后使用三元运算符 ?:[]来确保始终返回数组的原因。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

现在,我们可以向Api发出get请求,如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

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

在此示例中,我们使用新的do_remote_get函数向JSONPlaceholder Api 发出Get请求,并获取一些(假)文章。然后,我们遍历文章并回显其标题。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

注意:在此示例中,我们从do_remote_get函数中获取了对象数组。如果您希望将对象作为关联数组,则可以将true作为seccond参数传递给json_decode函数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

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

发出提交(Post)请求

在上面的示例中,我们使用wp_remote_get从远程Api获取一些文章。接下来,我们将处理提交请求,以在远程Api上创建文章。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

  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. return json_decode(wp_remote_retrieve_body($response)) ?: [];
  25. }

对于Post请求,我们创建一个名为do_remote_post的新函数,该函数与do_remote_get函数相似,但是第二个参数$data保留要发送到远程Api的数据。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

do_remote_post函数中,我们现在使用wp_remote_post函数发出请求。wp_remote_post函数接受相同的参数,和wp_remote_get一一对应。对于arguments数组,我们传递了一个额外的参数 body ,并将$data数组变量传递给了它。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

现在,我们可以发出提交请求,以在Api上创建一个新文章,如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

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

在这里,我们使用do_remote_post函数向JSONPlaceholder Api发出发布请求,并向其传递网址/端点和代表我们要创建的发布的数组。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

最后,我们使用var_dump打印来自Api的响应。JSONPlaceholder Api将仅返回我们创建的文章的Json对象。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

注意: Api请求需要花费一些时间才能解决,因此最好将其缓存以加快页面加载速度。建议使用WordPress瞬态来缓存Api请求结果 。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/37640.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/37640.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定