URL Reader

This function will download the contents of any URL and return it as a string that you can save to a file or database.

It will use curl if it is installed. Otherwise it will use fopen/fread.

function url_reader($url,$referer=null) {
  if (function_exists('curl_init')) {
    $ch  =  curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($referer) {
      curl_setopt($ch, CURLOPT_REFERER, $referer);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
  }
  else {
    $data = '';
    $handle = fopen($url,'rb');
    while (!feof($handle)) {
      $data .= fread($handle, 8192);
    }
    fclose($handle);
  }
  return $data;
}

Reply

  • Allowed HTML tags: <b> <br> <p> <a> <strong> <cite> <em> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may use [img:xx] tags to display uploaded files or images inline.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options