从动态 url(不是 json 文件)请求 wp_remote_get

Request wp_remote_get from a dynamic url (not a json file)

我目前正在尝试使用 wp_remote_get 方法从外部源(不是 Wp 站点)获取数据到 Wp 站点。 我想从中获取数据的站点没有实际的 json 文件来进行调用,而是他们提供此 link 来进行请求 (https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139。 我正在使用以下代码拨打电话。 有没有办法用 html 元素解析 php 中的响应,以便我可以在我的网站上正确显示它?

如有任何帮助,我们将不胜感激。

$url = ' https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139';

                   $request = wp_remote_get( $url );

                   if(is_wp_error($request)) {
                       return false;
                   } else {
                       $body = $request['body'];
                   }
                   echo $body;

$api_response = json_decode( wp_remote_retrieve_body( $response ), true );

回应

 { "coord":{ "lon":159, "lat":35 }, "weather":[ { "id":500, "main":"Rain", "description":"light rain", "icon":"https://cdn.glitch.com/6e8889e5-7a72-48f0-a061-863548450de5%2F10n.png?1499366021399" } ], "base":"stations", "main":{ "temp":22.59, "pressure":1027.45, "humidity":100, "temp_min":22.59, "temp_max":22.59, "sea_level":1027.47, "grnd_level":1027.45 }, "wind":{ "speed":8.12, "deg":246.503 }, "rain":{ "3h":0.45 }, "clouds":{ "all":92 }, "dt":1499521932, "sys":{ "message":0.0034, "sunrise":1499451436, "sunset":1499503246 }, "id":0, "name":"", "cod":200 }

这有帮助吗?你在那里有 $response - 从未定义过。

$url = ' https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139';

$request = wp_remote_get( $url );

if(is_wp_error($request)) {
    return false;
} else {
    // Returns the $body as object
    $body = json_decode(wp_remote_retrieve_body($request));
}

echo '<div class="weather">' . $body->weather[0]->main .  '</div>';

echo '<pre>'; print_r( $body ); echo '</pre>';

您可以使用 $api response 来显示数据。检查下面的代码。我没有显示所有数据,但可以通过打印显示 echo "<pre>"; print_r($api_response); echo "</pre>";

<?php

$url = 'https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139';

$response = wp_remote_get( $url );

if( is_wp_error( $response ) ) {
    return false;
} else {
    $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
}

?>

<div>

    <h1>Weather reports</h1>

    <div>
        <label>Lattitude - </label> <?php echo $api_response['coord']['lat']; ?>,
        <label>Longitude - </label> <?php echo $api_response['coord']['lon']; ?>
    </div>

    <div>
        <h3>Weather</h3>
        <label><?php echo $api_response['weather'][0]['main']; ?><img src="<?php echo $api_response['weather'][0]['icon']; ?>"> - <?php echo $api_response['weather'][0]['description']; ?></label>
    </div>

    <ul>
        <li>Temp - <?php echo $api_response['main']['temp']; ?></li>
        <li>Feels like - <?php echo $api_response['main']['feels_like']; ?></li>
        <li>Temp min - <?php echo $api_response['main']['temp_min']; ?></li>
        <li>Temp max - <?php echo $api_response['main']['temp_max']; ?></li>
        <li>Pressure - <?php echo $api_response['main']['pressure']; ?></li>
        <li>Humidity - <?php echo $api_response['main']['humidity']; ?></li>
    </ul>

    <div>
        <h3>Wind</h3>
        <ul>
            <li>Speed - <?php echo $api_response['wind']['speed']; ?></li>
            <li>Deg - <?php echo $api_response['wind']['deg']; ?></li>
            <li>Gust - <?php echo $api_response['wind']['gust']; ?></li>
        </ul>
    </div>

</div>

上面的代码显示了这样的东西。您可以根据您的要求进行修改。