GuzzleHttp - 获取 JSON 编码 body
GuzzleHttp - Get JSON encoded body
我为我的公司创建了一个 API 客户端来从我们的分销商那里获取订单。我需要使用 PUT 确认下载订单返回给他们。 PUT 工作正常,但我在确认我的确认时收到错误消息。
使用 Postman,我收到一条 JSON body 消息。
当我返回确认时,出现以下错误:
Type error: Argument 1 passed to GuzzleHttp\Client::send() must
implement interface Psr\Http\Message\RequestInterface, instance of
GuzzleHttp\Psr7\Response given, called in
/var/www/orders/app/Http/Controllers/edi/OrderController.php on line 86
这是第 86 行:
$response = $client->send($apirequest);
相关代码:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Psr7\Stream;
use Illuminate\Support\Facades\Input;
use Response;
use XmlParser;
use Psr\Http\Message\RequestInterface;
public function orderConfirm()
{
$uri = config('services.orders.orderack');
$formdata = Input::all();
$orders = Input::get('orders');
try {
$client = new GuzzleHttpClient([
'headers'=> [
'Authorization' => '$user',
'ContractID' => '$contract',
'Content-Type' => 'application/json']
]);
$apirequest = $client->request('PUT', $uri,
['body' => json_encode(
[
$orders
]
)]
);
$response = $client->send($apirequest);
$contents = (string) $response->getBody();
return $contents;
}
catch (RequestException $ex) {
//Exception Handling
echo $ex;
}
Postman 的输出是:
"Number of Orders Acknowledged: 1"
从 SO 上的其他帖子,这个:
$contents = (string) $response->getBody();
是让 body 和其他人解决问题的方法,但它对我不起作用。
显然我还遗漏了一些东西!
调用 $client->request()
实际上执行了请求(这就是为什么它返回 GuzzleHttp\Psr7\Response
的实例)而不是构建请求对象以供稍后发送。你不需要告诉客户发送任何东西,因为它已经被发送了;您只需要将 $response
变量设置为调用 $client->request()
.
的值
这可以在他们的 PSR7 文档的 Body example 中看到。
$response = $client->request('GET', 'http://httpbin.org/get');
要手动构建请求对象,您必须使用其构造函数创建 GuzzleHttp\Psr7\Request
的实例,如 documented under Requests.
// Create a request using a completely custom HTTP method
$request = new \GuzzleHttp\Psr7\Request('MOVE', 'http://httpbin.org/move');
echo $request->getMethod();
// MOVE
我为我的公司创建了一个 API 客户端来从我们的分销商那里获取订单。我需要使用 PUT 确认下载订单返回给他们。 PUT 工作正常,但我在确认我的确认时收到错误消息。 使用 Postman,我收到一条 JSON body 消息。
当我返回确认时,出现以下错误:
Type error: Argument 1 passed to GuzzleHttp\Client::send() must
implement interface Psr\Http\Message\RequestInterface, instance of
GuzzleHttp\Psr7\Response given, called in
/var/www/orders/app/Http/Controllers/edi/OrderController.php on line 86
这是第 86 行:
$response = $client->send($apirequest);
相关代码:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Psr7\Stream;
use Illuminate\Support\Facades\Input;
use Response;
use XmlParser;
use Psr\Http\Message\RequestInterface;
public function orderConfirm()
{
$uri = config('services.orders.orderack');
$formdata = Input::all();
$orders = Input::get('orders');
try {
$client = new GuzzleHttpClient([
'headers'=> [
'Authorization' => '$user',
'ContractID' => '$contract',
'Content-Type' => 'application/json']
]);
$apirequest = $client->request('PUT', $uri,
['body' => json_encode(
[
$orders
]
)]
);
$response = $client->send($apirequest);
$contents = (string) $response->getBody();
return $contents;
}
catch (RequestException $ex) {
//Exception Handling
echo $ex;
}
Postman 的输出是:
"Number of Orders Acknowledged: 1"
从 SO 上的其他帖子,这个:
$contents = (string) $response->getBody();
是让 body 和其他人解决问题的方法,但它对我不起作用。
显然我还遗漏了一些东西!
调用 $client->request()
实际上执行了请求(这就是为什么它返回 GuzzleHttp\Psr7\Response
的实例)而不是构建请求对象以供稍后发送。你不需要告诉客户发送任何东西,因为它已经被发送了;您只需要将 $response
变量设置为调用 $client->request()
.
这可以在他们的 PSR7 文档的 Body example 中看到。
$response = $client->request('GET', 'http://httpbin.org/get');
要手动构建请求对象,您必须使用其构造函数创建 GuzzleHttp\Psr7\Request
的实例,如 documented under Requests.
// Create a request using a completely custom HTTP method
$request = new \GuzzleHttp\Psr7\Request('MOVE', 'http://httpbin.org/move');
echo $request->getMethod();
// MOVE