I have this code:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'ip' => $_SERVER['REMOTE_ADDR'],
'country' => 'http://ip-api.com/json/'.$_SERVER['REMOTE_ADDR'],
'password' => bcrypt($data['password']),
'secret_question' => $data['secret_question'],
'question_answer' => $data['question_answer'],
]);
}
}
This is register function in laravel controller called AuthController.php
This line 'ip' => $_SERVER['REMOTE_ADDR'],
works fine, I'm gettig user's IP. This http://ip-api.com/json/YOURipADDRESS API which detects not only location but also it detects some more stuff based on IP address. I only need to get countryCode
from that API and store it to DB. How to do it correctly in this file ?
EDIT
This is my function right now but DB country is empty.
protected function create(array $data)
{
$user_details = json_decode(file_get_contents('http://ip-api.com/json/'.$_SERVER['REMOTE_ADDR']));
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'ip' => $_SERVER['REMOTE_ADDR'],
'country' => $user_details->country,
'password' => bcrypt($data['password']),
'secret_question' => $data['secret_question'],
'question_answer' => $data['question_answer'],
]);
}
$user_details = json_decode(file_get_contents('http://ip-api.com/json/your-ip-address'))
Now you have all details in $user_details
as json. You can now use it to store to your DB.
$user_details->country //to get country
In your AuthController.php
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'ip' => $_SERVER['REMOTE_ADDR'],
'country' => $this->getCountry($_SERVER['REMOTE_ADDR']),
'password' => bcrypt($data['password']),
'secret_question' => $data['secret_question'],
'question_answer' => $data['question_answer'],
]);
}
protected function getCountry($ip)
{
return json_decode(file_get_contents('http://ip-api.com/json/your-ip-address'))->country;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.