虽然fopen和file_get_contents都可以直接给url参数获取内容,有时会出现以下错误:
failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
采用curl获取url数据就可以避免这种时不时的错误了
function geturldata($url) { $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10); $content = curl_exec($ch); return $content; }