-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapifunc.php
103 lines (79 loc) · 2.45 KB
/
apifunc.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
<?php
// https://apifunc.github.io/php/apifunc.php
// curl https://apifunc.github.io/php/apifunc.php --output apifunc.php
/**
* @param array $func_url_array
* @param $callback
* @param string $local_path
* @return mixed
*
* @throws Exception
*/
function apifunc(array $func_url_array, $callback, $local_path = '.apifunc')
{
/** @var string $func_url */
foreach ($func_url_array as $func_url) {
$file_name = basename($func_url);
// IF exist in current folder
if (file_exists($file_name)) {
include_once($file_name);
continue;
}
// If exist PATH
if (!empty($local_path)) {
if (!file_exists($local_path)) {
if (!mkdir($local_path) && !is_dir($local_path)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $local_path));
}
}
$local_path = $local_path . DIRECTORY_SEPARATOR;
}
// FILE
$path = $local_path . $file_name;
// download if not exist
if (!file_exists($path)) {
// check if URL exist
if (!url_exists($func_url)) {
throw new Exception("The Url: " . $func_url . " not exist ");
}
// check if File is writeable
$out = fopen($path, "wb");
if ($out == FALSE) {
throw new Exception("File not opened");
}
// echo "::url: ". $func_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $func_url);
curl_exec($ch);
if (!empty(curl_error($ch))) {
throw new Exception("Error for url: " . $func_url . " : " . curl_error($ch));
}
curl_close($ch);
//fclose($handle);
}
// if(!@include($path)) throw new Exception("Failed to include 'script.php'");
if (!file_exists($path)) {
throw new Exception("File: " . $path . " not exist");
}
// include
include_once($path);
}
return $callback($func_url_array);
}
/**
* @param $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;
}