php - Rewrite class with fopen and using curl instead in Laravel -


really new in laravel , still trying learn please me bit here. there 1 class pull data site. problem is using fopen in matter of server security function off must use curl.

const cache_key = 'rate'; public static function getrate() {     if (cache::has(self::cache_key)) {         return cache::get(self::cache_key);     }      $onehourtimestamp = \carbon\carbon::now()->addhours(1);     $tenminutestimestamp = \carbon\carbon::now()->addminutes(10);      $page = trim(file_get_contents('https://example.com/'));     $rates = json_decode($page, true);     if (!$rates) {         cache::put(self::cache_key, '-', $tenminutestimestamp);         return '-';     }      $rate = @$rates['1']['2'];     if (!$rate) {         cache::put(self::cache_key, '-', $tenminutestimestamp);         return '-';     }      cache::put(self::cache_key, $rate, $onehourtimestamp);     return $usdrate; } 

update:

like this?

function get_curl_content($url) {    $curl = curl_init();    curl_setopt($curl, curlopt_url, $url);    curl_setopt($curl, curlopt_returntransfer, true);    curl_setopt($curl, curlopt_header, false);    $result = curl_exec($curl);    curl_close($curl);   return $result; }  const cache_key = 'rate'; public static function getrate() { if (cache::has(self::cache_key)) {     return cache::get(self::cache_key); }  $onehourtimestamp = \carbon\carbon::now()->addhours(1); $tenminutestimestamp = \carbon\carbon::now()->addminutes(10);     $result = $this->get_curl_content('https://example.com/'); $rates = json_decode($result, true); if (!$rates) {     cache::put(self::cache_key, '-', $tenminutestimestamp);     return '-'; }  $rate = @$rates['1']['2']; if (!$rate) {     cache::put(self::cache_key, '-', $tenminutestimestamp);     return '-'; }  cache::put(self::cache_key, $rate, $onehourtimestamp); return $usdrate; } 

to contentos of webpage curl can create function , add class:

function get_curl_content($url) {    $curl = curl_init();    curl_setopt($curl, curlopt_url, $url);    curl_setopt($curl, curlopt_returntransfer, true);    curl_setopt($curl, curlopt_header, false);    $result = curl_exec($curl);    curl_close($curl);    return $result; } 

and can change

$page = trim(file_get_contents('https://example.com/')); 

for:

$page = $this->get_curl_content('https://example.com/'); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -