使用WordPress函数向远程api发出Get和Post请求
有时您可能希望向远程/外部 Api 发出请求以获取一些数据。也许您想在博客上显示最新的微博内容,或者您想从其他 wordpress 网站获取最新的文章。对于这些情况,WordPress 具有 wp_remote_get 和 wp_remote_post 函数。
<?php /** * do_remote_get. * * Make a get request to a remote api, * * @see https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/ * * @uses wp_remote_get() https://developer.wordpress.org/reference/functions/wp_remote_get/ * @uses json_decode() https://www.php.net/manual/en/function.json-decode.php * @uses wp_remote_retrieve_body() https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/ * * @param String $url The url/endpoint to call * @return Array */ function do_remote_get(String $url) { $response = wp_remote_get($url, array( 'httpversion' => '1.1', 'blocking' => true )); return json_decode(wp_remote_retrieve_body($response)) ?: []; }
在此代码段中,我们创建一个名为 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 请求,如下所示:
<?php $posts = do_remote_get('https://jsonplaceholder.typicode.com/posts/'); foreach ($posts as $post) { echo "<h2>{$post->title}</h2>"; }
在此示例中,我们使用新的 do_remote_get 函数向 JSONPlaceholder Api 发出 Get 请求,并获取一些(假)文章。然后,我们遍历文章并回显其标题。
注意:在此示例中,我们从 do_remote_get 函数中获取了对象数组。如果您希望将对象作为关联数组,则可以将 true 作为 seccond 参数传递给 json_decode 函数。
<?php return json_decode(wp_remote_retrieve_body($response), true) ?: []; ?>
发出提交(Post)请求
在上面的示例中,我们使用 wp_remote_get 从远程 Api 获取一些文章。接下来,我们将处理提交请求,以在远程 Api 上创建文章。
<?php /** * do_remote_post. * * Make a post request to a remote api, * * @see https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/ * * @uses wp_remote_post() https://developer.wordpress.org/reference/functions/wp_remote_post/ * @uses json_decode() https://www.php.net/manual/en/function.json-decode.php * @uses wp_remote_retrieve_body() https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/ * * @param String $url The url/endpoint to call * @param Array $data The data to send * @return Array */ function do_remote_post(String $url, Array $data = []) { $response = wp_remote_post($url, array( 'httpversion' => '1.1', 'blocking' => true, 'body' => $data )); return json_decode(wp_remote_retrieve_body($response)) ?: []; }
对于 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 上创建一个新文章,如下所示:
<?php $response = do_remote_post('https://jsonplaceholder.typicode.com/posts/', [ 'userId' => 1, 'title' => 'foo', 'body' => 'bar' ]); 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 大学 翻译整理。
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!邮箱:(332547532@qq.com)
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务 请大家谅解!
5. 如有链接无法下载、失效或广告,请点击右下方联系站长,可领回失去的金币,并额外有奖!
6. 如遇到加密压缩包,默认解压密码请在"下载框架提示方寻找",如遇到无法解压的请联系管理员!
黑域吧资源网 » 使用WordPress函数向远程api发出Get和Post请求