Laravel在注册时获取文件内容并保存到DB(Laravel getting file content and save to DB when registering)

编程入门 行业动态 更新时间:2024-10-27 11:17:17
Laravel在注册时获取文件内容并保存到DB(Laravel getting file content and save to DB when registering)

我有这个代码:

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 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; }

更多推荐

本文发布于:2023-04-27 12:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并保存   文件   内容   Laravel   DB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!