2

Situation

I got stuck while trying to use cURL to retrieve data from my other site

When I go to http://localhost/api/distributors

  • I can see all the json. :)

BUT When I use the CLI and run this command curl -i --user test:1234 http://localhost/api/distributors

I couldn't connect to it, or see any json at all.

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'));

It called DistributorController > api_index

and here is my api_index function

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.

12
  • Put debug echo with LINE FILE and see , add laravel tag Commented Dec 23, 2014 at 13:34
  • Route::get ? curl is not post ? Try Route::any Commented Dec 23, 2014 at 13:36
  • ok. I will try that now. Commented Dec 23, 2014 at 13:37
  • debug echo with LINE FILE, what do you mean by that ? Also Route::any doesn't work either, when I run this curl -i --user test:1234 http://localhost/api/distributors Commented Dec 23, 2014 at 13:41
  • Does echo curl_error($ch) say something? Commented Dec 23, 2014 at 13:52

1 Answer 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

  1. Add auth.basic

    // Auth Basic

    Route::filter('auth.basic', function() { return Auth::basic("username"); }); 
  2. Add api Filter

    // API Route::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); } } 
Sign up to request clarification or add additional context in comments.

Comments