0

How to read JSON from api url with laravel 5.5 while maintaining the MVC format.

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 ?

There is also a plain solution of decoding JSON though simple php like this :

$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 ?

is my question is not clear please comment to correct it. any help would be appreciated. Thanks in Advance !

5
  • There are plenty of way. Use can use repository methods to get data and later use Laravel Collection to convert it into collections for better handling. Commented Oct 6, 2017 at 5:43
  • @GaneshK what do you exactly mean by "repository methods", and what are the procedures to convert this into Laravel collections, can you give me detailed info on it? Commented Oct 6, 2017 at 5:46
  • Reposity method is nothing but design pattern, bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5, follow up for repository pattern. For converting array into collection use collect method of laravel. Commented Oct 6, 2017 at 5:50
  • @GaneshK this repository pattern is a whole new thing, which will do extra in laravel, thanks for the info, but is there any simpler way to read json from url and convert it into laravel models? Commented Oct 6, 2017 at 6:04
  • Its will help me to understand that why you want to convert your json data into Laravel mode ? Commented Oct 6, 2017 at 6:06

1 Answer 1

1

In laravel you can go for Guzzle Package.

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

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).

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.

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

Guzzle documentation

http://docs.guzzlephp.org/en/stable/

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

https://www.youtube.com/watch?v=y6Zz-kgf4L8

Sign up to request clarification or add additional context in comments.

4 Comments

Not close what I wanted to know, Please read my question again, I want to know the way of handling JSON regarding decoding it though Controller/Model while maintaining the MVC model in laravel.
if you want mvc pattern then you can create helper class and create methods to handle get post and other http request .yuo can go for guzzle library
guzzle library is working fine for me, but it is hard to find any tutorial resources on guzzle with laravel, can you give me video/documentation tutorial resources on it?
Thanks for the helpful links.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.