我有这个代码:
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'], ]); } }这是laravel控制器中的寄存器功能,称为AuthController.php
这行'ip' => $_SERVER['REMOTE_ADDR'],工作正常,我是用户的IP。 这个http://ip-api.com/json/YOURipADDRESS API不仅可以检测位置,还可以根据IP地址检测更多内容。 我只需要从该API获取countryCode并将其存储到DB。 如何在此文件中正确执行?
编辑
这是我现在的功能,但DB国家是空的。
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'], ]); }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'))现在你将$user_details所有细节$user_details作为json。 您现在可以使用它来存储到您的数据库。
$user_details->country //to get country在你的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; } $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 countryIn 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; }更多推荐
发布评论