我已经编辑了这个问题,因为我意识到自己完全走错了路,但是仍然有问题。
使用Guzzle,如何从不使用Laravel的商店服务器向使用Laravel的退货服务器发送JSON形式的对象?
我一直收到以下错误:
Client error: `POST https://returns.jdoe.blah.test/createReturn` resulted in a `419 unknown status`.
我认为这与我没有令牌的事实有关,但是我不知道该怎么办。我知道Laravel使用CSRF令牌,但我的商店服务器未使用该表格。
在商店服务器中,当用户下订单时,该订单将保存在对象“ $ order”中。我将以下代码添加到order_details.php,以尝试传递订单对象的两个特定属性:
$client = new Client();
$url = "https://returns.jdoe.blah.test/createReturn";
$post_data = array(
'orderId' => $order['aufnr'],
'customerId' => $order['kundennummer']
);
$data = json_encode($post_data);
$request = $client->post($url, array(
'content-type' => 'application/json'
));
$request->setBody($data);
$response = $request->send();
然后在我的Laravel项目中,我有:
web.php
Route::post('/createReturn', 'ProductReturnsController@createReturn');
ProductReturnsController.php
<?php
namespace App\Http\Controllers;
use App\ProductReturn;
use Illuminate\Http\Request;
class ProductReturnsController extends Controller
{
public function createReturn($json)
{
echo "hallo";
/* $jsonDecoded = json_decode($json);
$orderId = $jsonDecoded['orderId'];
echo $orderId;*/
return view('data');
}
}
data.blade.php
<html>
<head>
Test
</head>
<body>
This is a test page.
</body>
</html>
如果您需要我提供其他帮助以解决此问题,请随时提出。谢谢:)。
答案 0 :(得分:0)
您的问题的答案实际上在Guzzle文档的第一页上:http://docs.guzzlephp.org/en/stable/
您正在执行var_dump($ response),它实际上是您提出的请求的响应对象。该对象具有方法getBody(),
所以尝试做
dd($response->getBody());
相反。
答案 1 :(得分:0)
尝试dd($response->getBody()->getContents())
或dd((string) $response->getBody())
。响应主体是一个流对象,因此,如果要使用字符串,则必须执行其他方法调用。