-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlet_txt.php
106 lines (88 loc) · 1.43 KB
/
let_txt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
//namespace LetTxt;
/**
* @param array|string $url
* @param null $callback
* @return false|mixed|string
*
* @throws Exception
*/
function let_txt($url, $callback = null)
{
if (empty($url)) {
throw new Exception("Url: $url is empty");
}
$urls = [];
if (!is_array($url)) {
$urls[] = $url;
} else {
$urls = $url;
}
$txt = '';
foreach ($urls as $url_item) {
// check if URL exist
if (!url_exists($url_item)) {
throw new Exception("Url: " . $url_item . " not exist ");
}
// Check Content
$file = file_get_contents($url, true);
if (empty($file)) {
throw new Exception("Content from Url: $url is empty");
}
$txt .= $file;
}
if (is_callable($callback)) {
return $callback($txt);
}
return $txt;
}
/**
* Class LetTxt
*/
class LetTxt
{
/** @var array|mixed */
public $json = [];
/** @var string */
public $url = '';
/**
* LetTxt constructor.
* @param $url
*/
function __construct($url)
{
$this->url = $url;
$this->json = let_txt($url);
}
/**
* @return mixed
*/
function first()
{
return $this->json[0];
}
/**
* @param $callback
*/
function each($callback)
{
foreach ($this->json as $item) {
$callback($item);
}
}
}
/**
* @param string $url
* @return bool
*/
function url_exists($url)
{
if (curl_init($url) === false) {
return false;
}
$headers = @get_headers($url);
if (strpos($headers[0], '200') === false) {
return false;
}
return true;
}