Location: PHPKode > scripts > Epguides > epguides/CurlGetPage.class.php
<?php

    /*
    * Copyright (c) 2009, Simon Paarlberg (hide@address.com) PGPkey:CA83E278
    * All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met:
    *     * Redistributions of source code must retain the above copyright
    *       notice, this list of conditions and the following disclaimer.
    *     * Redistributions in binary form must reproduce the above copyright
    *       notice, this list of conditions and the following disclaimer in the
    *       documentation and/or other materials provided with the distribution.
    *     * Neither the name of the <organization> nor the
    *       names of its contributors may be used to endorse or promote products
    *       derived from this software without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY Simon Paarlberg ''AS IS'' AND ANY
    * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    * DISCLAIMED. IN NO EVENT SHALL Simon Paarlberg BE LIABLE FOR ANY
    * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

abstract class CurlGetPage {

	private $cookie = array();
	private $debug = false;
	private $cookieHandling = false;

	public function getPage($url, $post=''){
		
		$ch = curl_init($url);

		if($this->cookieHandling) {
			curl_setopt ($ch, CURLOPT_HEADER, true);
			curl_setopt ($ch, CURLOPT_VERBOSE, true);
		}
		curl_setopt ($ch, CURLOPT_NOBODY, false);
		curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); 
		if(!empty($post)){
			curl_setopt ($ch, CURLOPT_POST, 1);
			curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
		}

		if($this->cookieHandling) {

			// GETTIN' THE HOSTNAME
			$host = substr($url,strpos($url,'//')+2);
			$host = substr($host,0,strpos($host,'/'));
			if($this->debug) echo("-- $url\n");
		
			// SEINDING THE STORED COOKIES BACK!
			$setcookie='';
			if(count($this->cookie)>0){
				for(reset($this->cookie);$arr_host=key($this->cookie);next($this->cookie)){
					//echo("{$key}\n");
					if($host==$arr_host){
						$arr = current($this->cookie);
						
						for(reset($arr);$key=key($arr);next($arr)){
							$value = current($arr);
							if($this->debug) echo("   SETTING COOKIE! {$key}={$value}; path=/\n");
							$setcookie .= "{$key}={$value}; path=/; ";
						}
					}
				}
				curl_setopt ($ch, CURLOPT_COOKIE, $setcookie);
			} 

		}
			
		curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);

		ob_start();
		curl_exec ($ch);
		$html = ob_get_contents();
		ob_end_clean();
		
		curl_close ($ch);

		if(!$this->cookieHandling)
			return $html;
		
		// SUBSTRINGING THE HEADER CLEAN
		if(strpos($html,"<")>-1) $x_header = trim(substr($html,0,strpos($html,"<")));
		else $x_header = $html;
		//echo("\n\n$x_header\n\n");
		
		// REMEMBERING THE COOKIES
		$split = split("\n",$x_header);
		for($n=0;$n<count($split);$n++){
			$name = ""; $value = ""; 
			$split[$n]=trim($split[$n]);
			if(strpos($split[$n],":")>0){
				if(strpos($split[$n],"Set-Cookie")>-1){
					$name = substr($split[$n],0,strpos($split[$n],"="));
					$name = substr($name,12);
					$value = substr($split[$n],strpos($split[$n],"=")+1);
					$value = substr($value,0,strpos($value,";"));
					if($this->debug) echo("   GETTING COOKIE! {$name}={$value}; path=/\n");

					$this->cookie[$host][$name] = $value;
				} 
				if(strpos($split[$n],"Location")>-1) $location = trim(substr($split[$n],9));
			}
		}

		// IF REF-LOCATION THEN GO EXPLORE..
		if(!empty($location) AND !strpos($location,"//")) $location = dirname($url)."/".$location;
		if(!empty($location)) $html = $this->getpage($location);

		// CUT THE HEADER FROM THE PAGE
		if(strpos($html,"<")>-1) $html = trim(substr($html,strpos($html,"<")));

		return $html;
	}

	public function enableDebug() {
		$this->debug = true;
	}

	public function disableDebug() {
		$this->debug = false;
	}
	
	public function enableCookieHandling() {
		$this->cookieHandling = true;
	}

	public function disableCookieHandling() {
		$this->cookieHandling = false;
	}
	
}
Return current item: Epguides