Situation
I got stuck while trying to use cURL to retrieve data from my other site
我在使用cURL从另一个站点检索数据时遇到了麻烦
When I go to http://localhost/api/distributors
当我访问http://localhost/api/时
- I can see all the json. :)
- 我可以看到所有的json。:)
BUT When I use the CLI and run this command curl -i --user test:1234 http://localhost/api/distributors
但是,当我使用CLI并运行这个命令curl -i——用户测试:1234 http://localhost/api/。
I couldn't connect to it, or see any json at all.
我无法连接它,也看不到任何json。
I am not sure what went wrong. I am positive that I type the right username and password.
我不知道哪里出了问题。我确信我输入了正确的用户名和密码。
Details
Here is my route.
这是我的路线。
Route::get('/api/distributors', array('before' => 'auth.basic', 'uses'=>'DistributorController@api_index'));
路线:get(/ api /分销商,阵列(“之前”= >“身份验证。基本”、“使用”= >“DistributorController@api_index”);
It called DistributorController
> api_index
它叫做分布控制器> api_index
and here is my api_index
function
这是api_index函数
public function api_index()
{
$distributors = [];
foreach(Distributor::all() as $distributor)
{
$user = $distributor->user()->first();
$distributors[$distributor->id] = [
'user' => $user->toArray(),
'distributor' => $distributor->toArray(),
'contacts' => $distributor->contacts()->get()->toArray(),
'addresses' => $distributor->addresses()->get()->toArray()
];
}
$json_string = json_encode($distributors, JSON_PRETTY_PRINT);
return $json_string;
}
Questions
- I am not sure where should I stick the decode section of my code. Should it go into the same function of of
encoding
? - 我不确定我应该把代码的解码部分放在哪里。它应该和编码的功能一样吗?
It look like this :
它是这样的:
<h1>Decode</h1>
<?php
$ch = curl_init("http://localhost/api/distributors");
curl_setopt($ch, CURLOPT_USERPWD, "test:1234");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($ch);
curl_close($ch);
?>
<?php $distributor = json_decode($body, TRUE); ?>
<!-- Test -->
<li><?php echo $distributor['distributor']['company_name']; ?></li>
Note
I only have a problem when trying to connect using cURL.
我只是在使用cURL连接时遇到了一个问题。
1 个解决方案
#1
1
3 Main Things to fix :
- add this to your routes.php
Route::group(array('prefix' => 'api', 'before' => 'auth.basic|api'), function(){
Route::resource('url', 'UrlController');
});
- add 2 things in filters.php
-
Add
auth.basic
添加auth.basic
// Auth Basic
/ /基本身份验证
Route::filter('auth.basic', function() { return Auth::basic("username"); });
-
Add
api
Filter添加api过滤器
// API Route::filter('api', function() {
// API路由:filter(‘API’,function() {
$user = Auth::user(); if ($user){ // Let them in } else{ return Response::view('errors.404', array(), 404); }
});
});
- Create NEW controller
function and throw this in there ..
<?php
class UrlController extends \BaseController {
// public function index(){
// return Response::json(User::all());
// }
public function index()
{
$distributors = [];
foreach(Distributor::all() as $distributor)
{
$user = $distributor->user()->first();
$distributors[$distributor->id] = [
'user' => $user->toArray(),
'distributor' => $distributor->toArray(),
'contacts' => $distributor->contacts()->get()->toArray(),
'addresses' => $distributor->addresses()->get()->toArray()
];
}
return Response::json($distributors);
}
}
#1
1
3 Main Things to fix :
- add this to your routes.php
Route::group(array('prefix' => 'api', 'before' => 'auth.basic|api'), function(){
Route::resource('url', 'UrlController');
});
- add 2 things in filters.php
-
Add
auth.basic
添加auth.basic
// Auth Basic
/ /基本身份验证
Route::filter('auth.basic', function() { return Auth::basic("username"); });
-
Add
api
Filter添加api过滤器
// API Route::filter('api', function() {
// API路由:filter(‘API’,function() {
$user = Auth::user(); if ($user){ // Let them in } else{ return Response::view('errors.404', array(), 404); }
});
});
- Create NEW controller
function and throw this in there ..
<?php
class UrlController extends \BaseController {
// public function index(){
// return Response::json(User::all());
// }
public function index()
{
$distributors = [];
foreach(Distributor::all() as $distributor)
{
$user = $distributor->user()->first();
$distributors[$distributor->id] = [
'user' => $user->toArray(),
'distributor' => $distributor->toArray(),
'contacts' => $distributor->contacts()->get()->toArray(),
'addresses' => $distributor->addresses()->get()->toArray()
];
}
return Response::json($distributors);
}
}