$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json'; $areaJson = file_get_contents($url);//简单的get请求,没用curl $areaArr = json_decode($areaJson, true); $cityName = $areaArr['city'].'市'; $path = Config::get('city_cache_path'); //读取文件 $cityCache = fopen($path, "r"); $cityJson = fread($cityCache, filesize($path));//fread从一个文件里读取length长度的内容到一个字符串,如果紧紧想将一个文件内容读到字符串,file_get_contents,它的性能比fread好很多 $cityArr = json_decode($cityJson, true); $cityid = isset($cityArr[$cityName]) ? $cityArr[$cityName] : 1;