user4287698
user4287698

Reputation:

How to allow access to other site(s) to retrieve data from your site/database?

I have 2 sites. Let's call it :

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.

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

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.

Upvotes: 2

Views: 161

Answers (1)

user4276926
user4276926

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

  • query whatever you need
  • store them in array index
  • encode the data
  • make your json pretty before send out

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

Related Questions