我目前是这个GuzzleHttp的新手,我在其他站点上读过一些文章,他们教GuzzleHttp如何在API Request中工作。我有一个问题,如果我的api是
,为什么Guzzle会给我这样的错误$response = $client->get('/api/first_data');
如果我的api看起来正确了
$response = $client->get('https://play.geokey.org.uk/api/projects/77');
这是我的控制器:
<?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);
}
}
如果我浏览localhost:8000 / api / first_data,则API响应
我的API路线:
Route::get('first_data','ContentController@first_data');
我的Web.php路线:
Route::get('/fetch_first_data','ContentController@fetch_first_data');
答案 0 :(得分:1)
默认情况下,Guzzle不会假定所有请求都基于运行它的应用程序的URI。例如,如果您的应用程序在以下位置运行
https://example.org
然后您尝试致电
$client->get('/api/first_data');
枪口将不会认为您要打电话
$client->get('https://example.org/api/first_data');
Guzzle对正在运行的站点没有任何概念,只有它试图调用的端点。相反,您必须使用任一呼叫的完整 full uri
$client->get('https://example.org/api/first_data');
如上所述,或在客户端配置中设置基本URI
$client = new Client(['base_uri' => 'https://example.org']);
$response = $client->get('/api/first_data');
答案 1 :(得分:0)
添加 http:// 作为URI的前缀
$client = new Client(['base_uri' => 'http://localhost:8000']);
(确保)