首页 > 解决方案 > 为什么 Local Laravel api 正在通过其本地消费者进行调试

问题描述

我试图让本地 api 和本地网站(api 的消费者)在过去两天一起工作,但没有快乐。本地 API:apishop.test 本地消费者:frontendflex.test 当我连接到实时 api(如 fakestoreapi.com)时,它在本地消费者网站上运行良好。我自己的本地 api 和 fakestoreapi 都会在屏幕上生成有效的 json

我尝试了各种方法来与不起作用的本地 api 进行通信。

$res=json_decode(file_get_contents('http://apishop.test/products/' . $request->product) 错误.. file_get_contents( https://apishop.test/products/2 ):无法打开流:HTTP 请求失败!HTTP/1.1 500 内部服务器错误

$res = Http::acceptJson()->get('http://apishop.test/products/' . $request->product);

ErrorException 未定义属性:Illuminate\Http\Client\Response::$title(查看:C:\xampp\htdocs\frontendflex\resources\views\shop\product.blade.php)。这是有道理的,因为如果我 dd($res) 它是 500 内部服务器错误,例如 file_get_contents()

所以要卷曲...

       curl_setopt($handle, CURLOPT_URL, 'http://apishop.test/products/2');
       curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']??null);
       curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

       //$res= json_decode(curl_exec($handle), true); //Attempt to read property "title" on null
        //$res= curl_exec($handle); //Attempt to read property "title" on string

       curl_close($handle);

但是当我 dd($res) 它喷出疯狂的错误数据,顶部是......

Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table 'frontendflex.products' 不存在(SQL: select * from productswhere products. id= 2 limit 1) http://apishop.test/products /2

这里没有产品表,它在 api 网站上。我在这里运行 frontendflex.test。所以越来越好奇,我故意在api网站代码中造成了一个错误,它在消费者网站frontendflex.test上捡到了它。这里发生了什么 ??两个网站完全分开,但在 xampp 下。头疼了两天了。任何帮助表示赞赏

标签: laravelapiconnectionlocal

解决方案


无奈之下,我创建了一个全新的干净的 laravel 项目,只有一个控制器和一个访问 api 的路由,它工作正常。因此,我需要深入了解其他站点发生的情况。某处配置错误。

编辑:我发现由于某种原因,如果我通过 php artisan:serve ( http://127.0.0.1:8000/doda ) 访问消费者网站 (frontendflex.test),它访问 api 网站 apishop.test 没问题。奇怪,但并不美妙!!有没有人可以对此有所启发?

编辑:发现了一种解决方法。将消费者站点的端口更改为http://frontendflex.test:8080/doda

    NameVirtualHost *:8080

<VirtualHost 127.0.0.1:8080>
    DocumentRoot "C:/xampp/htdocs/frontendflex/public"
    ServerName frontendflex.test
</VirtualHost>

在 apache vhosts 文件中更改为这个这有效,但我想知道为什么其他方式不起作用。希望有一天这对某个地方的人有所帮助:)


推荐阅读