laravel 如何使用laravel 5.5从api url读取JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46598938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read JSON from api url with laravel 5.5
提问by Fahim Uddin
How to read JSON from api url with laravel 5.5 while maintaining the MVC format.
如何在保持 MVC 格式的同时使用 laravel 5.5 从 api url 读取 JSON。
I have searched a lot about it but could not find any on point answer.
我已经搜索了很多关于它,但找不到任何关于点的答案。
There are lots of ways to read/post JSON using ajax, but that will harm the architecture, so I want to use controller to parse JSON and then pass to view. or maybe is there a way to parse json into model first ?
有很多方法可以使用 ajax 读取/发布 JSON,但这会损害架构,所以我想使用控制器来解析 JSON,然后传递给视图。或者有没有办法先将 json 解析为模型?
There is also a plain solution of decoding JSON though simple php like this :
通过像这样的简单 php 解码 JSON 也有一个简单的解决方案:
$rUrl = 'http://example.com/api/products';
$data = json_decode(file_get_contents($rUrl), true);
return view('mydata')->withData($data);
But Is there any better way to handle this in laravel ? and in which other ways I can maintain header and JSON results in laravel ?
但是有没有更好的方法在 laravel 中处理这个问题?以及我可以通过哪些其他方式在 laravel 中维护标头和 JSON 结果?
is my question is not clear please comment to correct it. any help would be appreciated. Thanks in Advance !
我的问题不清楚请发表评论以纠正它。任何帮助,将不胜感激。提前致谢 !
回答by iCoders
In laravel you can go for Guzzle Package.
在 Laravel 中,您可以选择 Guzzle Package。
https://github.com/guzzle/guzzle
https://github.com/guzzle/guzzle
if you are using get request then you can go for file_get_contents.But if you are sending post request then better you can use curl
如果您使用的是 get 请求,那么您可以使用 file_get_contents。但是如果您发送的是 post 请求,那么您可以更好地使用 curl
file_get_contents- It is a function to get the contents of a file(simply view source items i.e out put html file contents).
file_get_contents- 这是一个获取文件内容的函数(简单地查看源项目,即输出 html 文件内容)。
curl- It is a library to do more operations, for example get the contents like file_get_contents, sending and receiving data from one site to another site and it also supports different types of protocols like http, https, ftp, gopher, telnet, dict, file, and ldap. curl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading HTTP form based upload, proxies, cookies.
curl- 它是一个执行更多操作的库,例如获取 file_get_contents 之类的内容,从一个站点向另一个站点发送和接收数据,它还支持不同类型的协议,如 http、https、ftp、gopher、telnet、dict,文件和 LDAP。curl 还支持 HTTPS 证书、HTTP POST、HTTP PUT、FTP 上传、基于 HTTP 表单的上传、代理、cookie。
If you are looking for mvc pattern then you can create static methods in helper class to handle different type of request so you can call these methods
如果您正在寻找 mvc 模式,那么您可以在辅助类中创建静态方法来处理不同类型的请求,以便您可以调用这些方法
Guzzle documentation
狂饮文档
http://docs.guzzlephp.org/en/stable/
http://docs.guzzlephp.org/en/stable/
http://itsolutionstuff.com/post/laravel-guzzle-http-client-post-request-exampleexample.html
http://itsolutionstuff.com/post/laravel-guzzle-http-client-post-request-exampleexample.html
http://www.expertphp.in/article/laravel-php-guzzle-http-client-get-and-post-request-example
http://www.expertphp.in/article/laravel-php-guzzle-http-client-get-and-post-request-example