user4287698
user4287698

Reputation:

How to retrieve json data from other site(s) and decoding it using cURL?

Situation

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

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

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

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.

Upvotes: 2

Views: 127

Answers (1)

user4279548
user4279548

Reputation:

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

Upvotes: 1

Related Questions