Mehravish Temkar
Mehravish Temkar

Reputation: 4365

Calling API of another laravel project

I have two laravel projects. One of them has an API

I am trying to get data using the API.

public function getSyncOrders() {
    $orders = Invoice::where('status', 0)->get();
    return response()->json([
        'data' => [
            'orders'                => $orders
        ]
    ], 200);      
}

I am trying to fetch data in the other laravel project.

public function syncOrders() {
    if(auth()->check()){
        Order::truncate();

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "http://project1.net/api/sync-orders",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 600,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET"
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            //echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
    }
    else{
        return redirect('/');
    }
}

But I get the error :

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project2.invoice' doesn't exist (SQL: select * from invoice where status = 0)

Project 1 has table invoice in database while project 2 has table orders in database.

If I use the url http://project1.net/api/sync-orders in browser, it returns data.

I need help to fix the curl request so that project 2 doesn't execute code and search in its own database but instead get the data from the API.

Upvotes: 3

Views: 4487

Answers (4)

It is a cache problem, please run these commands in order to fix your problems:

php artisan config:clear
php artisan config:cache
php artisan cache:clear
php artisan optimize

Thanks to Sergio Chamorro for the solution.

Upvotes: 1

Mehravish Temkar
Mehravish Temkar

Reputation: 4365

Changing DB_DATABASE in config and env to DB_DATABASE2 for project 2 fixed the problem.

Upvotes: 5

Saman Ahmadi
Saman Ahmadi

Reputation: 687

It seems in this url (http://project1.net/api/sync-orders) , Invoice model is connected to 'project2' database.

So in your .env file or your config in (config/database.php) change database to project1

Upvotes: 0

Aria Radmand
Aria Radmand

Reputation: 331

It's so weird that you are seeing that error. If you insist on doing the job only using curl, maybe it'd be better if you provide more details.

But I recommend start using Guzzle

composer require guzzlehttp/guzzle:~6.0

then your code will become like this:

public function syncOrders() {
    if(auth()->check()){
        $client = new \GuzzleHttp\Client(['base_uri' => 'http://project1.net/api/']);
        $response = $client->get('sync-orders')->send();
        dd($response->getBody(), $response); // you can do whatever you want with $response
    }

    return redirect('/');

}

Upvotes: 0

Related Questions