Python linux命令 apache Ubuntu Android php centos Firefox 开源 微软 Windows mysql linux HTML5 wordpress nginx google 程序员 java shell

php里常用的远程采集函数

/**
 * 获取远程url的内容
 * @param string $url
 * @return string
 */
function get_url_content($url) {
  if(function_exists(curl_init)) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
     
    $file_contents = curl_exec($ch);
    curl_close($ch);
  } else {
    $file_contents = file_get_contents($url);
  }
 
  return $file_contents;
}

测试

$url = 'http://www.baidu.com';
$a = get_url_content($url);
echo $a;

延伸阅读

评论

暂无评论

写评论