Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I created this class for my mediawiki word count project.</p> <pre><code>// Copyright PHPExperts.pro // License: Any user on Stackflow may use this code under the BSD License. /** * Web page datatype that holds all the various parts * and info about a web page. */ class WebPage { public $url; public $headers; public $body; public $text; public function __construct($url) { // 1. Bail out now if the CURL extension is not loaded. if (!in_array('curl', get_loaded_extensions())) { throw new Exception(WebPageException::MISSING_CURL); } // 2. Make sure the URL is valid. self::ensureValidURL($url); // 3. Store the URL. $this-&gt;url = $url; } /** * Determine if a URL is valid. * * @param string $url * @returns true if the URL is a string and is a valid URL. False, otherwise. */ public static function isURLValid($url) { return (is_string($url) &amp;&amp; filter_var($url, FILTER_VALIDATE_URL) !== false); } public static function ensureValidURL($url) { if (!self::isURLValid($url)) { throw new WebPageException(WebPageException::INVALID_URL, array($url)); } } // captureHeader() donated by bendavis78@gmail.com, // via http://us.php.net/curl_setopt_array private function captureHeader($ch, $header) { $this-&gt;headers[] = $header; return strlen($header); } public function fetchURL() { $ch = curl_init(); curl_setopt_array($ch, array(CURLOPT_URL =&gt; $this-&gt;url, CURLOPT_RETURNTRANSFER =&gt; 1, CURLOPT_HEADERFUNCTION =&gt; array($this, 'captureHeader'), CURLOPT_TIMEOUT =&gt; 5, ) ); $data = curl_exec($ch); curl_close($ch); if ($data === false || is_null($data) || $data == '') { throw new WebPageException(WebPageException::BLANK_URL, array($this-&gt;url)); } // TODO: Need to handle HTTP error messages, such as 404 and 502. $this-&gt;body = $data; // Uses code from php@wizap.dom $this-&gt;text = remove_HTML($data); } } </code></pre> <p>After you run <code>WebPage::captureHeader()</code> you then just foreach through <code>$this-&gt;headers</code>, and if you don't find a HTTP/1.0 403 Forbidden, you're good to go.</p> <p>This thoroughly answers your question, so I expect credit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload