Completed
Push — master ( 1deced...926e5e )
by Daniel
08:35
created

url_checker::get_headers()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4286
cc 3
eloc 7
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class url_checker
13
{
14 6
	public function exists($url, $curl = true)
15
	{
16 6
		if (!filter_var($url, FILTER_VALIDATE_URL))
17 6
		{
18 2
			return false;
19
		}
20
21 4
		$status = array();
22 4
		$headers = $this->get_headers($url, $curl);
23
24 4
		preg_match('/HTTP\/.* ([0-9]+) .*/', $headers , $status);
25
26 4
		return ($status[1] == 200) ? true : false;
27
	}
28
29 4
	protected function get_headers($url, $curl)
30
	{
31 4
		if (extension_loaded('curl') && $curl)
32 4
		{
33 2
			$headers = $this->curl_header($url);
34 2
		}
35
		else
36
		{
37 2
			$headers = get_headers($url);
38 2
			$headers = $headers[0];
39
		}
40
41 4
		return $headers;
42
	}
43
44
	// http://snipplr.com/view.php?codeview&id=61985
45 2
	protected function curl_header($url)
46
	{
47 2
		$ch = curl_init();
48
49 2
		curl_setopt($ch, CURLOPT_URL, $url);
50 2
		curl_setopt($ch, CURLOPT_HEADER, true);
51 2
		curl_setopt($ch, CURLOPT_NOBODY, true);
52 2
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53 2
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
54
55 2
		$info = curl_exec($ch);
56
57 2
		curl_close($ch);
58
59 2
		return $info;
60
	}
61
}
62