Reputation:
I have 2 sites. Let's call it :
A
B
Website B
have a nice list of data, and all of its relation.
I want to allow website A
to access website B
and load those data
.
Of course, Website A
will need some kind of credentials, api_keys, or password to access into website B
. Right ? I never done this.
Then after the credential match, website B
will return the data back to website A
as json file.
Then, website A
will receive that json file, and load them into HTML and display it.
I hope I am clear enough regarding what my goal is.
Here is what I've tried :
After doing some researches, I came across this site. I really liked it. I finished it all way.Now, I kind of get a sense of RESTful API a little more.
Now, moving on to code
In my filters.php
I modify
Route::filter('auth.basic', function()
{
return Auth::basic("username");
});
In my routes.php
Route::get('/authtest', array('before' => 'auth.basic', function(){
return return "It's work !";
}));
After, I run
Note
username
= firstuser
password
= first_password
C:\wamp\www\laravel-1
λ curl --user firstuser:first_password localhost/l4api/public/index.php/authtest
I see
It's work !
As of right now, it only return a string.
How do I do it if I want to return the whole users table in json format ?
So far, I have not use any api_key
at all ? Is that bad ?
Big thanks
to everyone who involve in this post.
Upvotes: 2
Views: 161
Reputation:
In your routes.php
you should add sth like this.
Route::get('/api/distributors', array('before' => 'auth.basic', 'uses'=>'DistributorController@api_index'));
}));
Controller
<?php
$user = $distributor->user()->first();
$Data = [
'user' => $user->toArray(),
'distributor' => $distributor->toArray(),
'contacts' => $distributor->contacts()->get()->toArray(),
'addresses' => $distributor->addresses()->get()->toArray()
];
$json_string = json_encode($Data, JSON_PRETTY_PRINT);
?>
Logics
Then, the other site will receive 1
json file that have all the data in it.
They will then need to decode it before , they can load all of them into HTML/PHP
format.
Upvotes: 1