调用 $ret = fopen_url($baseUrl.$v, '');
/**
** 定义curl封装函数
** @param $url
** @param $postParam
** return json
**/
function fopen_url($url, $postParam = "")
{
$file_content = '';
if (function_exists('curl_init')) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url); // 需要获取的URL地址
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); // 在发起连接前等待的时间,如果设置为0,则无限等待。
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($curl_handle, CURLOPT_FAILONERROR, 1); // 显示HTTP状态码,默认行为是忽略编号小于等于400的HTTP信息
// 在HTTP请求中包含一个"User-Agent: "头的字符串。
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0');
if ($postParam != "") {
// 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($curl_handle, CURLOPT_POST, 1);
// 全部数据使用HTTP协议中的"POST"操作来发送。要发送文件,在文件名前面加上@前缀并使用完整路径。这个参数可以通过urlencoded后的字符串类似'para1=val1¶2=val2&...'
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postParam);
}
$file_content = curl_exec($curl_handle);
if ($file_content === false) {
echo 'Curl error: ' . curl_error($curl_handle);
$file_content = '';
}
curl_close($curl_handle);
} elseif (function_exists('file_get_contents')) {
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0');
$file_content = @file_get_contents($url);
} elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))) {
$i = 0;
while (!feof($file) && $i++ < 1000) {
$file_content .= strtolower(fread($file, 4096));
}
fclose($file);
} else {
$file_content = '';
}
return $file_content;
}