Skip to content

Instantly share code, notes, and snippets.

@arungpisyadi
Created June 13, 2017 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arungpisyadi/698710c47a21f6d8109ffdb986739365 to your computer and use it in GitHub Desktop.
Save arungpisyadi/698710c47a21f6d8109ffdb986739365 to your computer and use it in GitHub Desktop.
openexchange.org using laravel
public function updateCurrencies()
{
$listed = DB::table('currencies')->get();
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://openexchangerates.org/api/latest.json?app_id=9dfc90f4fd60462d9088aa0039ccb30d&base=USD');
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
// echo $result->access_token;
$rates = $obj->rates;
foreach($listed as $curr){
$_curr = $curr->code;
$rate = $rates->$_curr;
$updated_at = date("Y-m-d H:i:s");
DB::table('currencies')
->where('id', $curr->id)
->update(['updatedAt' => $updated_at, 'rate' => $rate]);
echo "$_curr updated to $rate at $updated_at \n\n";
}
echo "Updating currencies finished";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment