Location: PHPKode > scripts > Comic Engine > comic-engine/comicloader.php
<?
/**
 * This class can fetch daily comic strips for a given set of comics from comics.com and gocomics.com
 * This class is a part of gopsop, the localized twitter for bangla speakers at http://beta.gopsop.com
 * 
 * @author 		Hasin Hayder
 * @since 		Mar 30, 2008
 * @copyright 	New BSD License
 * 
 * @example 
 * $cl = new ComicLoader()
 * $nancy = $cl->getComicUrl("nancy");
 * echo "Today's Nancy <br/><img src='{$nancy}' />";
 */
class ComicLoader
{

	/**
	 * list of comics
	 *
	 * @var array
	 */
	private $comics = array(
	"dilbert"=>"http://dilbert.com",
	"lila"=>1,
	"floandfriends"=>1,
	"agnes"=>1,
	"momma"=>1,
	"claireday"=>1,
	"dogeat"=>1,
	"dinetteset"=>1,
	"herbnjamaal"=>1,
	"onebighappy"=>1,
	"pickles"=>"http://www.comics.com/wash/pickles/index.html",
	"zack"=>1,
	"girls"=>1,
	"liberty"=>1,
	"pcnpixels"=>"http://www.comics.com/wash/pcnpixel/index.html",
	"wizardofid"=>1,
	"heathcliff"=>1,
	"44unionavenue"=>2,
	"9to5"=>2,
	"adamathome"=>2,
	"agnes"=>2,
	"andycapp"=>2,
	"animalcrackers"=>2,
	"annie"=>2,
	"theargylesweater"=>2,
	"askshagg"=>2,
	"bc"=>2,
	"badreporter"=>2,
	"baldo"=>2,
	"ballardstreet"=>2,
	"bigtop"=>2,
	"biographic"=>2,
	"bleeker"=>2,
	"bloomcounty"=>2,
	"bonanas"=>2,
	"bobthesquirrel"=>2,
	"boondocks"=>2,
	"bottomliners"=>2,
	"boundandgagged"=>2,
	"brainwaves"=>2,
	"brendastarr"=>2,
	"brewsterrockit"=>2,
	"broomhilda"=>2,
	"calvinandhobbes"=>2,
	"candorville"=>2,
	"cathy"=>2,
	"citizendog"=>2,
	"thecity"=>2,
	"clearbluewater"=>2,
	"cleats"=>2,
	"closetohome"=>2,
	"cornered"=>2,
	"deepcover"=>2,
	"dicktracy"=>2,
	"dinetteset"=>2,
	"dogeatdoug"=>2,
	"domesticabuse"=>2,
	"doodles"=>2,
	"doonesbury"=>2,
	"duplex"=>2,
	"theelderberries"=>2,
	"thefifthwave"=>2,
	"flightdeck"=>2,
	"floandfriends"=>2,
	"theflyingmccoys"=>2,
	"forbetterorforworse"=>2,
	"foxtrot"=>2,
	"foxtrotclassics"=>2,
	"fredbasset"=>2,
	"frogapplause"=>2,
	"thefuscobrothers"=>2,
	"garfield"=>2,
	"gasolinealley"=>2,
	"gilthorp"=>2,
	"gingermeggs"=>2,
	"heartofthecity"=>2,
	"heathcliff"=>2,
	"herbandjamaal"=>2,
	"housebroken"=>2,
	"hubertandabby"=>2,
	"idiotbox"=>2,
	"inthebleachers"=>2,
	"inkpen"=>2,
	"thekchronicles"=>2,
	"kudzu"=>2,
	"lacucaracha"=>2,
	"libertymeadows"=>2,
	"lio"=>2,
	"looseparts"=>2,
	"lostsheep"=>2,
	"luckycow"=>2,
	"meaningoflila"=>2,
	"themiddletons"=>2,
	"minimumsecurity"=>2,
	"naturalselection"=>2,
	"nestheads"=>2,
	"neurotica"=>2,
	"nonsequitur"=>2,
	"thenorm"=>2,
	"onaclaireday"=>2,
	"onebighappy"=>2,
	"opus"=>2,
	"theothercoast"=>2,
	"outofthegenepool"=>2,
	"overboard"=>2,
	"pickles"=>2,
	"thepinkpanther"=>2,
	"pluggers"=>2,
	"poochcafe"=>2,
	"popculture"=>2,
	"preteena"=>2,
	"pricklycity"=>2,
	"thequigmans"=>2,
	"reallifeadventures"=>2,
	"redandrover"=>2,
	"redmeat"=>2,
	"reynoldsunwrapped"=>2,
	"ronaldinhogaucho"=>2,
	"rubes"=>2,
	"shoe"=>2,
	"shoecabbage"=>2,
	"slowpoke"=>2,
	"smallworld"=>2,
	"spaceistheplace"=>2,
	"speedbump"=>2,
	"stateoftheunion"=>2,
	"stonesoup"=>2,
	"strangebrew"=>2,
	"suttonimpact"=>2,
	"sylvia"=>2,
	"tankmcnamara"=>2,
	"teenagemutantninjaturtles"=>2,
	"thickandthin"=>2,
	"tinysepuku"=>2,
	"tomthedancingbug"=>2,
	"toomuchcoffeeman"=>2,
	"trevor"=>2,
	"watchyourhead"=>2,
	"waylay"=>2,
	"weepals"=>2,
	"winniethepooh"=>2,
	"wizardofid"=>2,
	"workingitout"=>2,
	"yenny"=>2,
	"zackhill"=>2,
	"ziggy"=>2
	);

	/**
	 * Retrieve url of the daily comic strip for a given comic name. 
	 * You can also use Coral CDN by setting second parameter to true and 
	 * reduce the load on the service provider
	 *
	 * @param string $comic
	 * @param boolean $usecdn
	 * @return string
	 */
	public function getComicUrl($comic,$usecdn=true){
		//error_reporting(E_ALL);
		if (!array_key_exists($comic, $this->comics))
		$this->comics[$comic]=0;
		if (array_key_exists($comic, $this->comics))
		{
			$value = $this->comics[$comic];
			switch ($value)
			{
				case "0":
					$url = "http://comics.com/comics/{$comic}/index.html";
					break;
				case "1":
					$url = "http://comics.com/creators/{$comic}/index.html";
					break;
				case "2":
					$url = "http://www.gocomics.com/{$comic}/";
					break;
				default:
					$url = $value;
			}


			$comiccontent = $this->file_get_the_contents($url);
			$pattern = "~Attachments=([\w/.]+)~i";
			$pattern2 = "~(http://images.ucomics.com/comics/[\w\d/]+\.gif)~i";

			if (strpos($url,"gocomics")===false){
				if (preg_match($pattern, $comiccontent, $match))
				{
					if($usecdn)
					$imgurl = "http://comics.com.nyud.net".$match[1];
					else
					$imgurl = "http://comics.com".$match[1];
					return $imgurl;
				}
			}
			else
			{
				if (preg_match($pattern2, $comiccontent, $match))
				{
					$imgurl = $match[1];
					if($usecdn)
					$imgurl = str_replace("ucomics.com","ucomics.com.nyud.net",$imgurl);
					return $imgurl;
				}
			}


		}
	}

	/**
	 * Wrapper to retrieve content of a remote file using curl functions
	 *
	 * @param 	string $url
	 * @return 	string
	 */
	private function file_get_the_contents($url) {
		$ch = curl_init();
		$timeout = 100; // set to zero for no timeout
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$file_contents = curl_exec($ch);
		curl_close($ch);
		return $file_contents;
	}
}

?>
Return current item: Comic Engine