iwa-panda2

Manage Weather Data by International Weather Agency (Version 2)
Log | Files | Refs | README

Connect.php (968B)


      1 <?php
      2 namespace Model{
      3     class Connect{
      4 
      5         protected string $url;
      6         protected string $token;
      7         protected string $json;
      8 
      9         function __construct($url, $token){
     10             $this->url = $url;
     11             $this->token = $token;
     12         }
     13 
     14         function connect(){
     15             $ch = curl_init();
     16             if ($ch === false) {
     17                 throw new Exception('failed to initialize');
     18             }
     19 
     20             $headers = array(
     21                 "X-Token: $this->token", // Your token value
     22             );
     23 
     24             curl_setopt($ch, CURLOPT_URL, $this->url);
     25             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     26             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     27 
     28             $response = curl_exec($ch);
     29 
     30             if ($response === false) {
     31                 throw new Exception(curl_error($ch), curl_errno($ch));
     32             }
     33 
     34             curl_close($ch);
     35 
     36             return json_decode($response);
     37         }
     38     }
     39 }