url_checker::curl_header()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
c 1
b 0
f 0
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
	/**
15
	 * @param string $url
16
	 * @param bool $curl
17
	 * @return bool
18
	 */
19 6
	public function exists($url, $curl = true)
20
	{
21 6
		if (!filter_var($url, FILTER_VALIDATE_URL))
22 6
		{
23 2
			return false;
24
		}
25
26 4
		$status = array();
27 4
		$headers = $this->get_headers($url, $curl);
28
29 4
		preg_match('/HTTP\/.* ([0-9]+) .*/', $headers , $status);
30
31 4
		return ($status[1] == 200) ? true : false;
32
	}
33
34
	/**
35
	 * @param string $url
36
	 * @param bool $curl
37
	 * @return mixed
38
	 */
39 4
	protected function get_headers($url, $curl)
40
	{
41 4
		if (extension_loaded('curl') && $curl)
42 4
		{
43 2
			$headers = $this->curl_header($url);
44 2
		}
45
		else
46
		{
47 2
			$headers = @get_headers($url);
48 2
			$headers = $headers[0];
49
		}
50
51 4
		return $headers;
52
	}
53
54
	/**
55
	 * http://snipplr.com/view.php?codeview&id=61985
56
	 * @param string $url
57
	 * @return mixed
58
	 */
59 2
	protected function curl_header($url)
60
	{
61 2
		$info = false;
62
		if (($ch = curl_init()) !== false)
63 2
		{
64 2
			curl_setopt($ch, CURLOPT_URL, $url);
65 2
			curl_setopt($ch, CURLOPT_HEADER, true);
66 2
			curl_setopt($ch, CURLOPT_NOBODY, true);
67 2
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
68
			curl_setopt($ch, CURLOPT_TIMEOUT, 10);
69 2
70
			$info = curl_exec($ch);
71 2
72
			curl_close($ch);
73 2
		}
74
75
		return $info;
76
	}
77
}
78