Guest User

apiCode

a guest
Dec 29th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2.  
  3. $params = array(
  4.     'convert' => 'INR'
  5. );
  6.  
  7. $url = 'https://api.coinmarketcap.com/v1/ticker/ripple/';
  8.  
  9. $result = sendRequest($url, $params);
  10.  
  11. $response = json_decode($result, true);
  12.  
  13. function sendRequest($url, $data, $user='', $pass = ''){
  14.     $params = '';
  15.     foreach($data as $key=>$value)
  16.         $params .= $key.'='.$value.'&';
  17.  
  18.     $params = trim($params, '&');
  19.  
  20.     $ch = curl_init();
  21.  
  22.     curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
  23.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
  24.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
  25.     curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  26.     curl_setopt($ch, CURLOPT_HEADER, 0);
  27.  
  28.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  29.    
  30.     $result = curl_exec($ch);
  31.  
  32.     if(curl_errno($ch))  //catch if curl error exists and show it
  33.       return array('error' => curl_error($ch));
  34.     else{
  35.         curl_close($ch);
  36.       return $result;
  37.     }
  38. }
  39.  
  40. function nice_number($n) {
  41.     // first strip any formatting;
  42.     $n = (0+str_replace(",", "", $n));
  43.  
  44.     // is this a number?
  45.     if (!is_numeric($n)) return false;
  46.  
  47.     // now filter it;
  48.     if ($n > 1000000000000) return round(($n/1000000000000), 2).' T';
  49.     elseif ($n > 1000000000) return round(($n/1000000000), 2).' B';
  50.     elseif ($n > 1000000) return round(($n/1000000), 2).' M';
  51.     elseif ($n > 1000) return round(($n/1000), 2).' Thousnad';
  52.  
  53.     return number_format($n);
  54. }
  55.  
  56.  
  57.  ?>
  58.  
  59.  <!DOCTYPE html>
  60.  <html lang="en">
  61.  <head>
  62.     <meta charset="UTF-8">
  63.     <title>Coin Market API</title>
  64.  
  65.     <!-- Latest compiled and minified CSS -->
  66.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  67.  
  68.  </head>
  69.  <body>
  70.     <table class="table table-bordered table-striped" style="width: 600px;margin: 50px auto;">
  71.         <tr>
  72.             <th>Name</th>
  73.             <th>Price</th>
  74.             <th>24h Volume USD</th>
  75.         </tr>
  76.         <?php foreach ($response as $key => $coins): ?>
  77.             <tr>
  78.                 <td><?php echo $coins['name'] ?></td>
  79.                 <td>₹ <?php echo round($coins['price_inr'], 2) ?></td>
  80.                 <td>$ <?php echo nice_number( $coins['24h_volume_usd'] ) ?></td>
  81.             </tr>
  82.         <?php endforeach ?>
  83.     </table>
  84.  </body>
  85.  </html>
Add Comment
Please, Sign In to add comment