今天抓图时显示Maximum execution time of 120 seconds exceeded
后来才知道是访问超时,只需在文件内加一句话即可解决
set_time_limit(120);
里面的数字是时间限制,单位为秒,如果想让程序永不超时,将其设置为0即可;
访问汉字和带空格的url的注意事项:
需设置字体为GBK,并且将url中的空格进行转义为“20%”
设置方法
把gb2312置换成utf-8:
| $text =iconv( "GB2312" , "UTF-8" , $text ); |
附源代码:
function getImg($url = "", $filename = "") {
if(is_dir(basename($filename))) {echo "The Dir was not exits";
Return false;
}
//去除URL连接上面可能的引号
$url = preg_replace( '/(?:^[\'"]+|[\'"\/]+$)/', '', $url );
$hander = curl_init();
$fp = fopen($filename,'wb');
curl_setopt($hander,CURLOPT_URL,$url);
curl_setopt($hander,CURLOPT_FILE,$fp);
curl_setopt($hander,CURLOPT_HEADER,0);
curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($hander,CURLOPT_TIMEOUT,3600);
curl_exec($hander);
curl_close($hander);
fclose($fp);
Return true;
}