如何将“FILE_GET_CONTENTS”更改为“wp_Remote_Get”

时间:2020-06-16 作者:Heng W

我需要将我的一个插件函数更改为使用;wp\\u remote\\u get“;而不是;file\\u get\\u contents“文件”;虽然我发现类似的线程也有这个问题,但我仍然无法让它工作;wp\\u remote\\u get";。带“,”;file\\u get\\u contents“;,它工作得非常好,但带有;wp\\u remote\\u get“;,事实并非如此。

这就是我要更改的代码

private static function geolocate_via_api( $ip_address ){
    $response = file_get_contents( "http://www.geoplugin.net/json.gp?ip=" . $ip_address);
    return json_decode( $response, true );  
}
我尝试了下面的代码,但它显示了一个错误;警告:为foreach()提供的参数无效;第42行中,如下所示。

错误:

foreach ( $data as $key => $value) {
        $mo_data[ str_replace( \'geoplugin_\', \'\', $key ) ] = $value;
    }
我尝试的代码:

    private static function geolocate_via_api( $ip_address ){
    $response = wp_remote_get( esc_url_raw(\'http://www.geoplugin.net/json.gp?ip=\' . $ip_address) );     
    return json_decode( wp_remote_retrieve_body( $response ), true );   
}
如果我能得到任何帮助,我将不胜感激。干杯

1 个回复
SO网友:Tom J Nowell

First, there is no error checking on any of the code, both the file_get_contents code, and the wp_remote_get code. Don\'t just assume it worked, check!

$response = file_get_contents( "http://www.geoplugin.net/json.gp?ip=" . $ip_address);
if ( false === $response ) {
    // it failed, abort!

Three steps, first, make the request:

$response = wp_remote_get( $url );

Note that we aren\'t using esc_url_raw, escaping is for enforcing assumptions on output, this is not browser output. If needed, use urlencode on the IP.

Then, we want to perform error checking. Your code performs no error checking of any kind, even in the file_get_contents example, it just assumes it worked. This is incorrect. Don\'t just assume functions work, check for error values.

So before we use the result:

if ( is_wp_error( $response ) ) {
    // it\'s an error! Abort!

Here we can look at the error message in the response object to see what went wrong. Take a look at the WP_Error documentation for how to do that.

Then, look inside the $response to see what the result was. $response contains several items:

  • \'headers\' (string[]) Array of response headers keyed by their name.
  • \'body\' (string) Response body.
  • \'response\' (array) Data about the HTTP response.
  • \'code\' (int|false) HTTP response code.
  • \'message\' (string|false) HTTP response message.
  • \'cookies\' (WP_HTTP_Cookie[]) Array of response cookies.
  • \'http_response\' (WP_HTTP_Requests_Response|null) Raw HTTP response object.

So to check what the HTTP response code is:

echo $response[\'code\'];

Here, you want the body, and to make it easier, wp_remote_retrieve_body

$body = wp_remote_retrieve_body( $result )
// or..
$body = $result[\'body\'];

Then we can json_decode. Note that I did not nest the function calls, that would make it really difficult to debug, and impossible to perform error checks.

Don\'t forget that json_decode needs error handling too! Don\'t just assume it worked.

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。