国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP cURL AJAX 代理問題
怪我咯
怪我咯 2017-04-10 14:40:48
0
1
351

執(zhí)行一次時是正常的,短時間(小于一秒)內連續(xù)請求多次就會出現(xiàn)只能成功執(zhí)行一條請求,后面的請求就會報錯說未收到回應或者收到多條相同的回應,可執(zhí)行代碼測試。

代碼如下:

PHP

<?php
$url = 'http://api.openweathermap.org/data/2.5/weather';

$query = filter_input(INPUT_GET, 'q');
$query || exit;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?q=' . $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = json_decode(curl_exec($ch)) ?: curl_getinfo($ch);
curl_close($ch);

header("Content-Type: application/json", true, 200);
echo json_encode($response);
exit;

HTML

<!doctype html><html><head><title>Get Weather</title><style>table{border-collapse:collapse;}td,th {border:1px solid #ccc; padding: 3px 5px;}</style></head><body><table><thead><tr><th>name</th><th>id</th><th>main</th><th>description</th><th>icon</th></tr></thead><tbody></tbody></table><script src="http://cdn.staticfile.org/jquery/2.1.0/jquery.js"></script><script>
(function($) {
    var cities = ['Shanghai,CN', 'Chongqing,CN', 'Wuhan,CN', 'Guangzhou,CN', 'Shanghai,CN'];
    $.each(cities, function(_, city) {
        getWeather(city);
    });

    function getWeather(city) {
        $.getJSON('./ajax_proxy.php?q=' + city, function(resp) {
            var table = $('table');
            $.each(resp.weather, function(_, o) {
                var tr = $('<tr>');
                tr.append($('<td>').html(resp.name));
                $.each(o, function(k, v) {
                    tr.append($('<td>').html(v));
                });
                tr.appendTo(table.find('tbody'))
            });
        });
    }
})(window.jQuery);
</script><body></html>
怪我咯
怪我咯

走同樣的路,發(fā)現(xiàn)不同的人生

reply all(1)
大家講道理

我覺得應該是API這邊做了限制,請看OpenWeatherMap API官網(wǎng)上寫的:

How to work with us effectively

These are several recommendations how to work with our free service in more effective way:

  • Do not send requests more then 1 time per 10 minutes from one device.The weather is changing not so frequently as usual.
  • Use the name of the server as api.openweathermap.org. Please never use the IP of the server.
  • If possible please use city ID or city name instead of city coordinates. It is let us use cash server more effective.
  • The service is absolutely free and has some limitation of capacity. So if you do not get respond from server please do not try to repeat your request immediately, please repeat it in 10 min. Also please store your previous request data.
  • If you need secured SLA please contact us.

以及價目表上的情況,明確表明API有頻率限制了。而且是申請了API的情況下。像你這樣直接就抓接口的人肯定有很多,也就是共用一個接口的人很多,所以更甚。所以正確的做法是去老老實實的申請一個Key啦,然后按照倒數(shù)第二條寫的一樣,按照規(guī)定頻率去做查詢然后存儲數(shù)據(jù),自己網(wǎng)站這邊訪問只需要讀取存儲好的數(shù)據(jù)就好啦。

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template