使用 Guzzle 原始发送 Body
Send Body as raw using Guzzle
我正在尝试使用 Guzzle 向我的网络服务发送 POST 请求。此服务接受 body 作为原始数据。当我使用邮递员但我不使用 Guzzle 时它工作正常。使用 Guzzle 时,我只获得了网络服务描述,因为我将网络服务 URL 放入浏览器。
这是我的代码:
$body = "CA::Read:PackageItems (CustomerId='xxxxxx',AllPackages=TRUE);";
$headers = [
....
....
];
$client = new Client();
$response = $client->request('POST', 'http://172.19.34.67:9882/TisService',$headers,$body);
echo $body = $response->getBody();
似乎headers或body没有通过。
这样试试
$response = $client->request('POST', 'http://172.19.34.67:9882/TisService',['headers' => $headers, 'body' => $body]);
我最近不得不第一次实现 Guzzle
,它是一个使用起来相当简单的库。
首先我创建了一个新客户端
// Passed in our options with just our base_uri in
$client = new Client(["base_uri" => "http://example.com"]);
然后我创建了一个 POST
请求,但不是我使用 new Request
而不是 $client->request(...
的方式。不过,这对我使用 new Request
来说并不重要。
// Create a simple request object of type 'POST' with our remaining URI
// our headers and the body of our request.
$request = new Request('POST', '/api/v1/user/', $this->_headers, $this->body);
所以本质上它看起来像:
$request = new Request('POST', '/api/v1/user/', ['Content-Type' => "application/json, 'Accept' => "application/json], '{"username": "myuser"}');
$this->headers
是我们请求 header 的简单 key-value 对数组,确保设置 Content-Type
header 和 $this->body
是一个简单的字符串 object,在我的例子中它形成了一个 JSON body.
然后我可以简单地调用 $client->send(...
方法来发送请求,例如:
// send would return us our ResponseInterface object as long as an exception wasn't thrown.
$rawResponse = $client->send($request, $this->_options);
$this->_options
是一个简单的 key-value 对数组,对于 headers
数组来说也很简单,但这包括请求的 timeout
之类的东西。
对我来说,我创建了一个名为 HttpClient
的简单 Factory
object,它为我构建了整个 Guzzle
请求,这就是为什么我只创建一个新的 Request
object 而不是调用 $client->request(...
也会发送请求。
要将数据作为原始数据发送到 json_encode 您的 $data
数组并在请求中发送它 body
.
$request = new Request(
'POST',
$url,
['Content-Type' => 'application/json', 'Accept' => 'application/json'],
\GuzzleHttp\json_encode($data)
);
$response = $client->send($request);
$content = $response->getBody()->getContents();
使用 guzzle 请求 GuzzleHttp\Psr7\Request;
和客户端 GuzzleHttp\Client
我正在尝试使用 Guzzle 向我的网络服务发送 POST 请求。此服务接受 body 作为原始数据。当我使用邮递员但我不使用 Guzzle 时它工作正常。使用 Guzzle 时,我只获得了网络服务描述,因为我将网络服务 URL 放入浏览器。 这是我的代码:
$body = "CA::Read:PackageItems (CustomerId='xxxxxx',AllPackages=TRUE);";
$headers = [
....
....
];
$client = new Client();
$response = $client->request('POST', 'http://172.19.34.67:9882/TisService',$headers,$body);
echo $body = $response->getBody();
似乎headers或body没有通过。
这样试试
$response = $client->request('POST', 'http://172.19.34.67:9882/TisService',['headers' => $headers, 'body' => $body]);
我最近不得不第一次实现 Guzzle
,它是一个使用起来相当简单的库。
首先我创建了一个新客户端
// Passed in our options with just our base_uri in
$client = new Client(["base_uri" => "http://example.com"]);
然后我创建了一个 POST
请求,但不是我使用 new Request
而不是 $client->request(...
的方式。不过,这对我使用 new Request
来说并不重要。
// Create a simple request object of type 'POST' with our remaining URI
// our headers and the body of our request.
$request = new Request('POST', '/api/v1/user/', $this->_headers, $this->body);
所以本质上它看起来像:
$request = new Request('POST', '/api/v1/user/', ['Content-Type' => "application/json, 'Accept' => "application/json], '{"username": "myuser"}');
$this->headers
是我们请求 header 的简单 key-value 对数组,确保设置 Content-Type
header 和 $this->body
是一个简单的字符串 object,在我的例子中它形成了一个 JSON body.
然后我可以简单地调用 $client->send(...
方法来发送请求,例如:
// send would return us our ResponseInterface object as long as an exception wasn't thrown.
$rawResponse = $client->send($request, $this->_options);
$this->_options
是一个简单的 key-value 对数组,对于 headers
数组来说也很简单,但这包括请求的 timeout
之类的东西。
对我来说,我创建了一个名为 HttpClient
的简单 Factory
object,它为我构建了整个 Guzzle
请求,这就是为什么我只创建一个新的 Request
object 而不是调用 $client->request(...
也会发送请求。
要将数据作为原始数据发送到 json_encode 您的 $data
数组并在请求中发送它 body
.
$request = new Request(
'POST',
$url,
['Content-Type' => 'application/json', 'Accept' => 'application/json'],
\GuzzleHttp\json_encode($data)
);
$response = $client->send($request);
$content = $response->getBody()->getContents();
使用 guzzle 请求 GuzzleHttp\Psr7\Request;
和客户端 GuzzleHttp\Client