Reputation: 1459
I currently new for this GuzzleHttp, I read some post in other site they teach how it GuzzleHttp Work in API Request. I have question why Guzzle give me error like this if my api is this
$response = $client->get('/api/first_data');
if my api look this show correct
$response = $client->get('https://play.geokey.org.uk/api/projects/77');
This my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use DateTime;
use Illuminate\Support\Facades\Storage;
use GuzzleHttp\Client;
class ContentController extends Controller
{
//
public function first_data() {
$table_slider = DB::select('SELECT content_id,link,file,slider_sorting,content_pages FROM content_structure as cs LEFT JOIN (SELECT cid,file FROM content_upload_assets) cua ON cs.content_id = cua.cid WHERE content_pages = ? AND cs.status = ? ORDER BY content_id DESC ',[
'Slider',
'Active'
]);
return response()->json($table_slider);
}
public function fetch_first_data() {
$client = new Client(['base_uri' => 'localhost:8000']);
$response = $client->get('/api/first_data');
$body = $response->getBody()->getContents();
$project = json_decode($body);
dd($project);
}
}
The API response if i browse the localhost:8000/api/first_data
My API route:
Route::get('first_data','ContentController@first_data');
My Web.php Route:
Route::get('/fetch_first_data','ContentController@fetch_first_data');
Upvotes: 2
Views: 11003
Reputation: 687
Add http:// as prefix to your URI
$client = new Client(['base_uri' => 'http://localhost:8000']);
(make sure)
Upvotes: 2
Reputation: 3040
Guzzle does not assume that all requests are based on, by default, the URI of the application you run it in. For example, if your application is running at
https://example.org
and you try to call
$client->get('/api/first_data');
Guzzle will not assume you mean to call
$client->get('https://example.org/api/first_data');
Guzzle does not have any concept of what site you are running in, only the endpoint of what it is trying to call. Instead, you must use the complete full uri of the call using either
$client->get('https://example.org/api/first_data');
as mentioned above, or set the base URI in the Client config
$client = new Client(['base_uri' => 'https://example.org']);
$response = $client->get('/api/first_data');
Upvotes: 1