有時您可能希望向遠程/外部Api發(fā)出請求以獲取一些數據。也許您想在博客上顯示最新的微博內容,或者您想從其他WordPress網站獲取最新的文章。對于這些情況,WordPress具有wp_remote_get和wp_remote_post?函數。

發(fā)出獲取(Get)請求
<?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)) ?: [];
}
在此代碼段中,我們創(chuàng)建一個名為do_remote_get的新函數,該函數除了一個名為$url的參數外,該參數必須為string類型。在我們的新函數中,我們使用wp_remote_get函數發(fā)出實際的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發(fā)出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?發(fā)出Get請求,并獲取一些(假)文章。然后,我們遍歷文章并回顯其標題。
注意:在此示例中,我們從do_remote_get函數中獲取了對象數組。如果您希望將對象作為關聯數組,則可以將true作為seccond參數傳遞給json_decode函數。
<?php return json_decode(wp_remote_retrieve_body($response), true) ?: []; ?>
發(fā)出提交(Post)請求
在上面的示例中,我們使用wp_remote_get從遠程Api獲取一些文章。接下來,我們將處理提交請求,以在遠程Api上創(chuàng)建文章。
<?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請求,我們創(chuàng)建一個名為do_remote_post的新函數,該函數與do_remote_get函數相似,但是第二個參數$data保留要發(fā)送到遠程Api的數據。
在do_remote_post函數中,我們現在使用wp_remote_post函數發(fā)出請求。wp_remote_post函數接受相同的參數,和wp_remote_get一一對應。對于arguments數組,我們傳遞了一個額外的參數 body ,并將$data數組變量傳遞給了它。
現在,我們可以發(fā)出提交請求,以在Api上創(chuàng)建一個新文章,如下所示:
<?php
$response = do_remote_post('https://jsonplaceholder.typicode.com/posts/', [
'userId' => 1,
'title' => 'foo',
'body' => 'bar'
]);
var_dump($response);
在這里,我們使用do_remote_post函數向JSONPlaceholder Api發(fā)出發(fā)布請求,并向其傳遞網址/端點和代表我們要創(chuàng)建的發(fā)布的數組。
最后,我們使用var_dump打印來自Api的響應。JSONPlaceholder Api將僅返回我們創(chuàng)建的文章的Json對象。
注意:?Api請求需要花費一些時間才能解決,因此最好將其緩存以加快頁面加載速度。建議使用WordPress瞬態(tài)來緩存Api請求結果 。
以上內容出自: https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/ ,由 WordPress大學 翻譯整理。
拓展閱讀
《 WordPress HTTP API 指南 》:本系列將詳細講解 WordPress HTTP API,涉及到常見的原創(chuàng)獲取數據 wp_remote_get 和遠程提交數據 wp_remote_post 等功能。




