Php 如何向远程laravel web应用发送请求
我正在尝试使用GuzzleHttp将来自raspberry pi的请求发送到laravel项目 我试过了 我的剧本:Php 如何向远程laravel web应用发送请求,php,laravel,guzzle,Php,Laravel,Guzzle,我正在尝试使用GuzzleHttp将来自raspberry pi的请求发送到laravel项目 我试过了 我的剧本: <?php require 'vendor/autoload.php'; $client = new GuzzleHttp\Client(); $res = $client->request('GET', 'http://mydomain.fake/api/print-invoice'); echo $res->getBody(); 我犯了这个错误 Fat
<?php
require 'vendor/autoload.php';
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://mydomain.fake/api/print-invoice');
echo $res->getBody();
我犯了这个错误
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error:
`GET http://mydomain.fake/api/print-invoice` resulted in a `404 Not Found`
response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head>
<title>404 Not Found</title> </head><body> <h1>Not Found (truncated...) in
/opt/lampp/htdocs/print-script/vendor/guzzlehttp/guzzle/src/Exception
/RequestException.php:113 Stack trace: #0 /opt/lampp/htdocs/print-script
/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception
\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /opt/lampp/htdocs/print-script/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp
\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 /opt/lampp/htdocs/print-script
/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise
\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3
/opt/lampp/htdocs/print-script/vendor/guzzlehttp/promises
/src/TaskQueue.php(47): GuzzleHttp\Promise\ in /opt/lampp/htdocs/print-script
/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113
更新:问题是路径错误。确保
php artisan serve
处于打开状态,并在请求中使用json\u解码
echo json_解码($res->getBody()->getContents()) 确保
php artisan serve
处于打开状态,并在请求中使用json\u解码
echo json_解码($res->getBody()->getContents()) 正如你所看到的,Guzzle说没有找到页面。您是否创建了虚拟主机?您是否在web.php中编写了该路由?如果是这样,它将不会起作用。因为当你使用它时,它不会工作。您已经为路由调用了api.php文件。也许这就是为什么你没有得到它?我首先在web.php中编写了路由,并使用了不带/api/的url,得到了与你看到的Guzzle说找不到页面相同的错误。您是否创建了虚拟主机?您是否在web.php中编写了该路由?如果是这样,它将不会起作用。因为当你使用它时,它不会工作。您已经为路由调用了api.php文件。也许这就是为什么你没有得到它?我首先在web.php中编写了路由,并使用不带/api/的url,得到了相同的错误
json\u decode
对404页的响应没有任何作用。是的,当数据来自服务器时,它将应用于响应部分我在live test server上测试json\u decode
不起作用404页面响应的任何内容。是的,当数据来自服务器时,它将应用于响应部分。我正在live test server上测试
{
public function autoPrint(){
$response = 'test request';
return response()->json($response);
}
}
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error:
`GET http://mydomain.fake/api/print-invoice` resulted in a `404 Not Found`
response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head>
<title>404 Not Found</title> </head><body> <h1>Not Found (truncated...) in
/opt/lampp/htdocs/print-script/vendor/guzzlehttp/guzzle/src/Exception
/RequestException.php:113 Stack trace: #0 /opt/lampp/htdocs/print-script
/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception
\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /opt/lampp/htdocs/print-script/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp
\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 /opt/lampp/htdocs/print-script
/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise
\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3
/opt/lampp/htdocs/print-script/vendor/guzzlehttp/promises
/src/TaskQueue.php(47): GuzzleHttp\Promise\ in /opt/lampp/htdocs/print-script
/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>302 Found</title>
</head>
<body>
<h1>Found</h1>
<p>The document has moved <a href="http://mydomain.fake/api/print-invoice">here</a>.</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at mydomain.fake Port 80</address>
</body>
</html>