Advertisement
RamRaider

GeoNames - Earthquakes & Weather

Mar 11th, 2023 (edited)
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.49 KB | Source Code | 0 0
  1. <?php
  2.    
  3.     function curl( $url=NULL, $options=NULL, $headers=false ){
  4.         $vbh = fopen('php://temp', 'w+');
  5.  
  6.         session_write_close();
  7.        
  8.         $curl=curl_init();
  9.         curl_setopt( $curl, CURLOPT_URL, trim( $url ) );
  10.         curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
  11.         curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  12.         curl_setopt( $curl, CURLOPT_FAILONERROR, true );
  13.         curl_setopt( $curl, CURLOPT_HEADER, false );
  14.         curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
  15.         curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  16.         curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
  17.         curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
  18.         curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
  19.         curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' );
  20.         curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
  21.         curl_setopt( $curl, CURLOPT_ENCODING, '' );
  22.         curl_setopt( $curl, CURLOPT_VERBOSE, true );
  23.         curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
  24.         curl_setopt( $curl, CURLOPT_STDERR, $vbh );
  25.        
  26.  
  27.         /* Assign runtime parameters as options to override defaults if needed. */
  28.         if( isset( $options ) && is_array( $options ) ){
  29.             foreach( $options as $param => $value ) {
  30.                 curl_setopt( $curl, $param, $value );
  31.             }
  32.         }
  33.         /* send any headers with the request that are needed */
  34.         if( $headers && is_array( $headers ) ){
  35.             curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
  36.         }
  37.         /* Execute the request and store responses */
  38.         $res=(object)array(
  39.             'response'  =>  curl_exec( $curl ),
  40.             'status'    =>  curl_getinfo( $curl, CURLINFO_RESPONSE_CODE ),
  41.             'info'      =>  (object)curl_getinfo( $curl ),
  42.             'errors'    =>  curl_error( $curl )
  43.         );
  44.         rewind( $vbh );
  45.         $res->verbose=stream_get_contents( $vbh );
  46.         fclose( $vbh );
  47.         curl_close( $curl );
  48.             sa
  49.         return $res;
  50.     }
  51.    
  52.    
  53.    
  54.    
  55.    
  56.    
  57.    
  58.    
  59.    
  60.    
  61.    
  62.    
  63.    
  64.     if( $_SERVER['REQUEST_METHOD']=='POST' ){
  65.         ob_clean();
  66.         $json=array();
  67.        
  68.         $baseurl='http://api.geonames.org';
  69.         $args=array(
  70.             'formatted' =>  true,
  71.             'username'  =>  'XXXXXXXX',
  72.             'style'     =>  'full'
  73.         );
  74.        
  75.         switch( $_POST['task'] ){
  76.             case 'weatherObservations': $endpoint='weatherJSON'; break;
  77.             case 'earthquakes': $endpoint='earthquakesJSON'; break;
  78.         }
  79.        
  80.         unset( $_POST['task'] );
  81.         $args=array_merge( $args, $_POST );
  82.        
  83.        
  84.         $url=sprintf('%s/%s?%s', $baseurl, $endpoint, strtolower( http_build_query( $args ) ) );
  85.        
  86.         $res=curl( $url );
  87.         if( $res->status==200 ){
  88.            
  89.             header('Content-Type: application/json');
  90.             exit( $res->response );
  91.         }
  92.        
  93.         exit( $json );
  94.     }
  95. ?>
  96. <!DOCTYPE html>
  97. <html lang="en">
  98.     <head>
  99.         <meta charset="UTF-8">
  100.         <title>Task | GeoNames</title>
  101.  
  102.     </head>
  103.     <body>
  104.  
  105.         <div id="preloader"></div>
  106.  
  107.         <h1>Weather coordinates</h1>
  108.         <form method="POST">
  109.             <label>North<input name="North" type="number" step="0.01" value=2 /></label>
  110.             <label>South<input name="South" type="number" step="0.01" value=3 /></label>
  111.             <label>East<input name="East" type="number" step="0.01" value=4 /></label>
  112.             <label>West<input name="West" type="number" step="0.01" value=5 /></label>
  113.             <input type="button" data-task='weatherObservations' />
  114.         </form>
  115.  
  116.         <h1>Earthquake coordinates</h1>
  117.         <form method="POST">
  118.             <label>North<input name="North" type="number" step="0.01" value=2 /></label>
  119.             <label>South<input name="South" type="number" step="0.01" value=3 /></label>
  120.             <label>East<input name="East" type="number" step="0.01" value=4 /></label>
  121.             <label>West<input name="West" type="number" step="0.01" value=5 /></label>
  122.             <input type="button" data-task="earthquakes" />
  123.         </form>
  124.        
  125.        
  126.        
  127.        
  128.         <table>
  129.             <tr>
  130.                 <td>API Name</td>
  131.                 <td>API Description</td>
  132.                 <td>Go</td>
  133.             </tr>
  134.             <tr>
  135.                 <td>Weather By coordinates</td>
  136.                 <td>Weather</td>
  137.                 <td><button>Submit</button></td>
  138.             </tr>
  139.             <tr>
  140.                 <td>Get countries</td>
  141.                 <td>All countries</td>
  142.                 <td><button id="countries">Submit</button></td>
  143.             </tr>
  144.             <tr>
  145.                 <td>Earthquakes</td>
  146.                 <td>Earthquakes by longitude and latitude</td>
  147.                 <td><button>Submit</button></td>
  148.             </tr>
  149.         </table>
  150.  
  151.         <div>
  152.             <span id="clouds"></span>
  153.             <span id="temperature"></span>
  154.             <span id="station"></span>
  155.         </div>
  156.  
  157.         <div>
  158.             <span id="languages"></span>
  159.             <span id="distance"></span>
  160.             <span id="countryCode"></span>
  161.         </div>
  162.  
  163.         <div>
  164.             <span id="depth"></span>
  165.             <span id="lng"></span>
  166.             <span id="magnitude"></span>
  167.             <span id="lat"></span>
  168.         </div>
  169.  
  170.  
  171.         <script>
  172.  
  173.             const el=(id)=>document.getElementById(id);
  174.            
  175.             const settext=(id,v)=>{
  176.                 if( el(id) ) el(id).textContent=v;
  177.             };
  178.            
  179.             const callback=( task, json )=>{
  180.                 let obj=json[ task ][0];
  181.                
  182.                 switch( task ){
  183.                     case 'weatherObservations':
  184.                         settext('clouds',obj.clouds);
  185.                         settext('temperature',obj.temperature);
  186.                         settext('station',obj.station);
  187.                     break;
  188.                     case 'earthquakes':
  189.                         settext('depth',obj.depth);
  190.                         settext('lng',obj.lng);
  191.                         settext('lat',obj.lat);
  192.                         settext('magnitude',obj.magnitude);
  193.                     break;
  194.                 }
  195.             };
  196.            
  197.             document.addEventListener('click',e=>{
  198.                 if( e.target.dataset.task!=null && e.target instanceof HTMLInputElement && e.target.type=='button' ){
  199.                    
  200.                     let task=e.target.dataset.task;
  201.                    
  202.                     let fd=new FormData( e.target.closest('form') );
  203.                         fd.set('task',task);
  204.                        
  205.                     fetch( location.href,{ method:'post', body:fd } )
  206.                         .then(r=>r.json())
  207.                         .then(json=>{
  208.                             callback.call( this, task, json );
  209.                         })
  210.                 }
  211.             })
  212.         </script>  
  213.     </body>
  214. </html>
  215.  
  216.  
Tags: JavaScript php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement