knasenn /
vaderanax
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Aiur\Curl; |
||||
| 4 | |||||
| 5 | use Anax\Commons\ContainerInjectableInterface; |
||||
| 6 | use Anax\Commons\ContainerInjectableTrait; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * To ease rendering a page consisting of several views. |
||||
| 10 | */ |
||||
| 11 | class Curl implements ContainerInjectableInterface |
||||
| 12 | { |
||||
| 13 | use ContainerInjectableTrait; |
||||
| 14 | |||||
| 15 | private $urlipstack; |
||||
| 16 | private $urldarksky; |
||||
| 17 | private $apikeyIpstack; |
||||
| 18 | private $apikeyDarksky; |
||||
| 19 | |||||
| 20 | |||||
| 21 | |||||
| 22 | 2 | public function __construct() |
|||
| 23 | { |
||||
| 24 | //Set url |
||||
| 25 | 2 | $this->urlipstack = "http://api.ipstack.com"; |
|||
| 26 | 2 | $this->urldarksky = "https://api.darksky.net/forecast"; |
|||
| 27 | //Get apikey |
||||
| 28 | 2 | $apikeys = require __DIR__ . "/../../config/apikeys.php"; |
|||
| 29 | //Set apikey |
||||
| 30 | 2 | $this->apikeyIpstack = $apikeys["ipstack"]; |
|||
| 31 | 2 | $this->apikeyDarksky = $apikeys["darksky"]; |
|||
| 32 | 2 | } |
|||
| 33 | |||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * Set the view to be used for the layout. |
||||
| 37 | * |
||||
| 38 | * @param array $view configuration to create up the view. |
||||
| 39 | * |
||||
| 40 | * @return $this |
||||
| 41 | */ |
||||
| 42 | 2 | public function getCurlAdress($ip, $valid) |
|||
| 43 | { |
||||
| 44 | 2 | $checkValid = explode(' ', $valid); |
|||
| 45 | |||||
| 46 | 2 | if ($checkValid[2] == "not") { |
|||
| 47 | 2 | $result = array_map('floatval', explode(',', $ip)); |
|||
| 48 | 2 | if (count($result) == 2) { |
|||
| 49 | 2 | if ($result[0] <= 90 && $result[1] <= 90 && $result[0] >= -90 && $result[1] >= -90) { |
|||
| 50 | 2 | return $result; |
|||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 51 | } else { |
||||
| 52 | $result = array("testing", "testing123"); |
||||
| 53 | return $result; |
||||
|
0 ignored issues
–
show
|
|||||
| 54 | } |
||||
| 55 | } else { |
||||
| 56 | 2 | $result = array("testing", "testing123"); |
|||
| 57 | 2 | return $result; |
|||
|
0 ignored issues
–
show
|
|||||
| 58 | } |
||||
| 59 | } else { |
||||
| 60 | 2 | $curl = curl_init(); |
|||
| 61 | 2 | curl_setopt($curl, CURLOPT_URL, "{$this->urlipstack}/{$ip}?access_key={$this->apikeyIpstack}"); |
|||
|
0 ignored issues
–
show
It seems like
$curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 62 | 2 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|||
| 63 | 2 | $result = curl_exec($curl); |
|||
|
0 ignored issues
–
show
It seems like
$curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 64 | 2 | $decoded_result = json_decode($result); |
|||
| 65 | 2 | return $decoded_result; |
|||
| 66 | } |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | |||||
| 70 | /** |
||||
| 71 | * Set the view to be used for the layout. |
||||
| 72 | * |
||||
| 73 | * @param array $view configuration to create up the view. |
||||
| 74 | * |
||||
| 75 | * @return $this |
||||
| 76 | */ |
||||
| 77 | 2 | public function getCurlTemp($lat, $long, $date) |
|||
| 78 | { |
||||
| 79 | 2 | if ($date == "plusweek") { |
|||
| 80 | 2 | $url = "$this->urldarksky/{$this->apikeyDarksky}"; |
|||
| 81 | 2 | $curl = curl_init(); |
|||
| 82 | 2 | curl_setopt($curl, CURLOPT_URL, "$url/{$lat},{$long}"); |
|||
|
0 ignored issues
–
show
It seems like
$curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 83 | 2 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|||
| 84 | 2 | $result = curl_exec($curl); |
|||
|
0 ignored issues
–
show
It seems like
$curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 85 | 2 | $decoded_result = json_decode($result); |
|||
| 86 | |||||
| 87 | // Changes UNIX-date to "normal" |
||||
| 88 | 2 | foreach ($decoded_result->daily->data as $key => $value) { |
|||
| 89 | 2 | $timestamp=$decoded_result->daily->data[$key]->time; |
|||
| 90 | 2 | $decoded_result->daily->data[$key]->time = gmdate("Y-m-d", $timestamp); |
|||
| 91 | } |
||||
| 92 | |||||
| 93 | 2 | return $decoded_result; |
|||
| 94 | 2 | } elseif ($date == "minusmonth") { |
|||
| 95 | $month = [ |
||||
| 96 | 2 | strtotime('-1 day'), strtotime('-2 day'), strtotime('-3 day'), |
|||
| 97 | 2 | strtotime('-4 day'), strtotime('-5 day'), strtotime('-6 day'), |
|||
| 98 | 2 | strtotime('-7 day'), strtotime('-8 day'), strtotime('-9 day'), |
|||
| 99 | 2 | strtotime('-10 day'), strtotime('-11 day'), strtotime('-12 day'), |
|||
| 100 | 2 | strtotime('-13 day'), strtotime('-14 day'), strtotime('-15 day'), |
|||
| 101 | 2 | strtotime('-16 day'), strtotime('-17 day'), strtotime('-18 day'), |
|||
| 102 | 2 | strtotime('-19 day'), strtotime('-20 day'), strtotime('-21 day'), |
|||
| 103 | 2 | strtotime('-22 day'), strtotime('-23 day'), strtotime('-24 day'), |
|||
| 104 | 2 | strtotime('-25 day'), strtotime('-26 day'), strtotime('-27 day'), |
|||
| 105 | 2 | strtotime('-28 day'), strtotime('-29 day'), strtotime('-30 day') |
|||
| 106 | ]; |
||||
| 107 | |||||
| 108 | 2 | $url = "$this->urldarksky/{$this->apikeyDarksky}/{$lat},{$long}"; |
|||
| 109 | $options = [ |
||||
| 110 | 2 | CURLOPT_RETURNTRANSFER => true, |
|||
| 111 | ]; |
||||
| 112 | |||||
| 113 | //Add all curl handles and remember theme |
||||
| 114 | //Init the multi curl dba_handler |
||||
| 115 | 2 | $mh = curl_multi_init(); |
|||
| 116 | 2 | $chAll = []; |
|||
| 117 | 2 | foreach ($month as $id) { |
|||
| 118 | 2 | $ch = curl_init("$url,$id"); |
|||
| 119 | 2 | curl_setopt_array($ch, $options); |
|||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 120 | 2 | curl_multi_add_handle($mh, $ch); |
|||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 121 | 2 | $chAll[] = $ch; |
|||
| 122 | } |
||||
| 123 | |||||
| 124 | //execute all queries simult |
||||
| 125 | // and cont. when compl. |
||||
| 126 | 2 | $running = null; |
|||
| 127 | do { |
||||
| 128 | 2 | curl_multi_exec($mh, $running); |
|||
| 129 | 2 | } while ($running); |
|||
| 130 | |||||
| 131 | //close the handles |
||||
| 132 | 2 | foreach ($chAll as $ch) { |
|||
| 133 | 2 | curl_multi_remove_handle($mh, $ch); |
|||
| 134 | } |
||||
| 135 | 2 | curl_multi_close($mh); |
|||
| 136 | |||||
| 137 | //done with result |
||||
| 138 | 2 | $response = []; |
|||
| 139 | 2 | foreach ($chAll as $ch) { |
|||
| 140 | 2 | $data = curl_multi_getcontent($ch); |
|||
| 141 | 2 | $response[] = json_decode($data, true); |
|||
| 142 | } |
||||
| 143 | 2 | foreach ($response as $key => $value) { |
|||
| 144 | 2 | $response[$key]["currently"]["time"] = gmdate("Y-m-d", $response[$key]["currently"]["time"]); |
|||
| 145 | } |
||||
| 146 | |||||
| 147 | 2 | return $response; |
|||
|
0 ignored issues
–
show
|
|||||
| 148 | } else { |
||||
| 149 | $url = "$this->urldarksky/{$this->apikeyDarksky}"; |
||||
| 150 | $curl = curl_init(); |
||||
| 151 | curl_setopt($curl, CURLOPT_URL, "$url/{$lat},{$long}"); |
||||
| 152 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||||
| 153 | $result = curl_exec($curl); |
||||
| 154 | $decoded_result = json_decode($result); |
||||
| 155 | return $decoded_result; |
||||
| 156 | } |
||||
| 157 | } |
||||
| 158 | } |
||||
| 159 |