ik ben bezig met het schrijven van een script om informatie uit een URL te halen.
Dit is nog maar het begin maar wil toch al nuttige hulp/informatie/gedachten om het script al te verbeteren voordat het definitief klaar is.
Omdat ik regelmatig geconfronteerd met problemen door allow_url_open, safe_mode enz op verschillende servers maar toch steeds alle informatie nodig heb, dacht ik om het mezelf gemakkelijker te maken door er een class voor te schrijven.
<?php
/****************************************\
Versie 1.0
Ophalen van URL informatie door middel van een class.
Het gebruik van deze class is vrij toegestaan voor iedereen.
-------------------------
Woordje uitleg:
Met deze class kan je informatie ophalen door het ingeven van een URL.
Je krijgt alle informatie te zien die je wenst, ook als je geen
toegang hebt tot je php.ini en je allow_url_fopen op uit (0, false, off)
staat. In sommige gevallen kan het ook zijn dat je safe_mode op enable staat
en/of je open_basedir, ook in deze class is er een oplossing voor gevonden.
-------------------------
Gebruiksaanwijzing:
SETTERS
->setFolder :: Zet de naam van de map waar inhoud bewaard moet worden
(true) : Maak map aan als deze niet bestaat
(false) : Geef een fout als map niet bestaat
->setNewName :: Wijzig de originele filename naar de nieuwe naam
(newName) : De naam die je wilt gebruiken
->setUrl :: Zet URL
(uri) : De link die je wilt gebruiken om informatie op te halen
GETTERS
->getHeaders :: Ophalen van header informatie
->getParse :: Ophalen van parse
->getContent :: Ophalen van inhoud
(true) : toont als array
(false) : toont als string
DOENERS
->save :: Bewaar de inhoud op eigen server
(boolean) :
- true : file overschrijven indien deze al bestaat
- false : toon een foutmelding indien deze file al bestaat
->unzip :: Unzip file indien een zipfile
\****************************************/
class getUrlInfo
{
// Public
public
$data,
$error,
$headers,
$url;
// Private
private
$allow_url_fopen,
$open_basedir,
$safe_mode;
// Protected
// Static
// Start het laden van nodige informatie
public function __construct(){
// Controleer INI en wijzig naar standaard indien nodig
$changeIni = array(
'allow_url_fopen' => '1',
'open_basedir' => NULL,
'safe_mode' => false,
);
foreach ($changeIni as $key => $value){
$setNow = ini_get($key);
if ($setNow !== $value){ ini_set($key, $value); }
$this->$key = ini_get($key);
$this->$key = !empty($this->$key) ? $this->$key : 0;
}
}
/**************************************************\
SETTERS
\**************************************************/
public function setUrl($uri=NULL){
$uri = trim($uri);
if (empty($uri))
{
// URL is leeg, toon een foutmelding
$this->error['setUrl'] = 'URL can\'t be empty';
} else {
// URL is gevuld
$this->url = $uri;
}
}
/**************************************************\
GETTERS
\**************************************************/
// Ophalen van header informatie
public function getHeaders()
{
if ($this->url)
{
if (!$this->headers = @get_headers($this->url, 1) or $this->allow_url_fopen == '0'){
$ch = curl_init($this->url);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_URL, $this->url);
@curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
@curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
@curl_setopt($ch, CURLOPT_HEADER, array('Content-Type:application/x-www-form-urlencoded'));
@curl_setopt($ch, CURLOPT_NOBODY, true);
$header = curl_exec($ch);
curl_close($ch);
$explode = explode("\r\n", $header);
foreach ($explode as $value){
$value = trim($value);
if (!empty($value))
{
$keyVal = explode(':', $value);
$keyVal[0] = isset($keyVal[0]) ? trim($keyVal[0]) : NULL;
$keyVal[1] = isset($keyVal[1]) ? trim($keyVal[1]) : NULL;
if (!empty($keyVal[0]) && !empty($keyVal[1]))
{
$this->headers[$keyVal[0]] = '';
for ($i=1; $i<count($keyVal); $i++)
{
if (!empty($this->headers[$keyVal[0]])){ $this->headers[$keyVal[0]] .= ':'; }
$this->headers[$keyVal[0]] .= $keyVal[$i];
}
}
elseif (!empty($keyVal[0]))
{
$this->headers[] = $keyVal[0];
}
}
}
}
} else {
$this->error['getHeaders'] = 'No URL found, please use ->setUrl() to contineu';
}
}
// Parse URL
public function getParse(){
if (empty($this->url))
{
$this->error['getParse'] = 'No URL found, please use ->setUrl() to contineu';
}
else
{
// Parse Now
$this->parse = parse_url($this->url);
if (!isset($this->parse['host']) or empty($this->parse['host']))
{
$this->parse = parse_url('//'.$this->url);
if (!empty($this->parse['path']) and empty($this->parse['host']))
{
$this->parse['host'] = ltrim($this->parse['path'], '/');
unset($this->parse['path']);
}
}
$this->parse['path'] = isset($this->parse['path']) ? ltrim($this->parse['path'], '/') : NULL;
// Get File
$explode = explode('/', $this->parse['path']);
$file = end($explode);
if (count(explode('.', $file)) < 2)
{
unset($file);
}
else
{
$this->parse['path'] = rtrim(str_replace($file, '', $this->parse['path']), '/');
$explode = explode('.', $file);
$this->parse['file']['extention'] = end($explode);
$this->parse['file']['filename'] = str_replace('.'.$this->parse['file']['extention'], '', $file);
}
// Get Keys in Query
if (isset($this->parse['query']))
{
$keys = explode('&', $this->parse['query']);
foreach($keys as $key => $value)
{
$exp = explode('=', $value);
$this->parse['key'][$exp[0]] = $exp[1];
}
}
$this->parse['user'] = isset($this->parse['user']) ? $this->parse['user'] : NULL;
$this->parse['pass'] = isset($this->parse['pass']) ? $this->parse['pass'] : NULL;
// Get Domainname
$this->parse['domain'] = trim($this->parse['host']);
$this->parse['domain'] = preg_replace('/^(http:\/\/)*(www.)*/is', '', $this->parse['domain']);
$this->parse['domain'] = preg_replace('/\/.*$/is', '', $this->parse['domain']);
$parts = explode('.', $this->parse['domain']);
while (array_shift($parts) !== NULL)
{
$hostname = trim(implode(".", $parts));
if (!empty($hostname) && checkdnsrr($hostname, "A")) { $this->parse['domain'] = $hostname; }
}
// Check DNS
if (empty($this->parse['domain']) or !checkdnsrr($this->parse['domain']))
{
$this->error['dns'] = 'Not A Valid URL !!';
}
}
}
// Ophalen van inhoud
public function getContent($setAsArray=false)
{
$data = NULL;
if (empty($this->url))
{
$this->error['getParse'] = 'No URL found, please use ->setUrl() to contineu';
}
elseif (!$data = @file_get_contents($this->url))
{
$ch = curl_init($this->url);
// curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $this->url);
$data = curl_exec($ch);
curl_close($ch);
}
if (!empty($data) and $setAsArray===true)
{
$this->data = explode("\n", $data); // Ontploffen naar lijn bij lijn
$this->data = array_map('trim', $this->data); // Scheren
$this->data = array_map('htmlentities', $this->data); // Toon de broncode
$this->data = array_filter($this->data, 'strlen'); // Verwijder alle NULL, FALSE en Lege Strings maar behoud de 0
}
elseif (!empty($data))
{
$this->data = $data;
}
}
}
// Start het ophalen van informatie
$urlInfo = new getUrlInfo;
$urlInfo->setUrl('http://www.phphulp.nl/');
$urlInfo->getHeaders();
$urlInfo->getParse();
$urlInfo->getContent(true);
// Toon informatie
print '<pre>'.PHP_EOL;
print_r($urlInfo);
print '</pre>'.PHP_EOL;
?>