duangai1916 2014-12-19 14:37
浏览 48
已采纳

如何允许访问其他站点以从您的站点/数据库中检索数据?

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.

  • 写回答

1条回答 默认 最新

  • dongluo3331 2014-12-22 19:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备