这两天我一直在试图弄清楚为什么这不起作用。基本上我在玩 TradeMe 的 API,他们在这里有一个 .json 文件:
链接1:http ://api.trademe.co.nz/v1/Listings/Latest.json
而不是耗尽我认为好的 API 速率限制......我将为我的 Web 服务器编写一个脚本,该脚本抓取这个文件并将其返回给我(我当然必须在本地缓存它,但我还没有完全到那里了)。假设我的 fetcher URL 是这样的:
链接 2:http://{MY URL}/trademe/fetcher.php?url=http://api.trademe.co.nz/v1/Listings/Latest.json
问题是...... cURL 为链接 2 返回给我的不是链接 1 返回给我的内容!每次我运行我的脚本(链接 2)时,它总是给我链接 1 的 JSON 文件的旧版本(我知道它是旧版本,因为我查看了 JSON 文件中的 ListingId 数字)。有时我运行链接 2,它给了我一个更新的 JSON,然后当我刷新脚本时,它又给了我旧的 JSON!(请注意,如果我直接在 Chrome 中访问链接 1,它总是会给我最新的 JSON 文件。我的脚本有问题吗?)
这是我的脚本:
<?php
set_time_limit(3600);
header("Content-Type: application/json");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_URL, $_GET['url']);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>
我基本上是通过 jQuery 的 $.getJSON 调用这个脚本。我什至在我的 index.php 中放入了 no cache headers 并放入了 $.ajaxSetup({ cache: false }); 但没有运气。
有任何想法吗?
(这可能会有所启发,但我还没有找到任何东西:http: //developer.trademe.co.nz/api-overview/)
干杯