guzzlehttp/guzzle 在 ->send() 之前发送 HTTP 请求
guzzlehttp/guzzle sends HTTP request before ->send()
我正在尝试使用 Guzzle 来使用 RESTful API. Reading the Guzzle documentation,我调用 get
和 post
等方法来配置请求,然后 send
实际上 运行 一个 HTTP 请求。
$request = $client->post('http://httpbin.org/post', array(), array(
'custom_field' => 'my custom value',
'file_field' => '@/path/to/file.xml'
));
$response = $request->send();
但是在我的例子中,似乎在调用 post
期间发送了一个没有字段的 HTTP 请求。这是我的代码
define('CUST_ID', 'blahblah');
define('API_KEY', 'verysecure');
// Construct the underlying Guzzle client
$oClient = new \GuzzleHttp\Client(
['base_url' =>
['http://api.postmates.com/{version}/', ['version' => 'v1']],
'defaults' => [
// HTTP Basic auth header, username is api key, password is blank
'auth' => [API_KEY, ''],
]]);
$oRq = $oClient->post(
"customers/" . CUST_ID . "/delivery_quotes",
[],
['pickup_address' => '232 E Manhattan Ave, Denver, CO 80203',
'dropoff_address' => '4400 Midwest St, Denver, CO 80205']);
发出的请求(没有 POST 参数)
POST /v1/customers/blahblah/delivery_quotes HTTP/1.1
Host: api.postmates.com
Authorization: Basic verysecure==
User-Agent: Guzzle/5.2.0 curl/7.37.1 PHP/5.6.0
Content-Length: 0
和回应
HTTP/1.1 400 BAD REQUEST
Content-Type: application/json
Date: Fri, 13 Feb 2015 07:32:16 GMT
Server: nginx/1.1.19
Content-Length: 205
Connection: keep-alive
{"kind": "error", "code": "invalid_params", "params": {"dropoff_address": "This field is required.", "pickup_address": "This field is required."}, "message": "The parameters of your request were invalid."}
所以看起来我正在验证,但没有传递任何 post 参数。我尝试使用 setPostField
方法设置 POST 字段,但没有成功。正如我所说,在那种情况下,似乎在调用 setPostField
之前发送了一个 HTTP 请求,在任何一种情况下,在我调用 send
.
之前发送了一个 HTTP 请求
我试过 guzzlehttp/guzzle 5.2.0 和 5.0.0。
哇,看起来我在看 2 份文档,一份用于 the newer GuzzleHttp\Client
, one for the older Guzzle\Http\Client
。与新库的最大区别在于,如果您想延迟调用 HTTP 请求,则需要调用 createRequest
。
新库下的工作代码
define('CUST_ID', 'blahblah');
define('API_KEY', 'verysecure');
// Construct the underlying Guzzle client
$oClient = new GuzzleHttp\Client(
['base_url' =>
['http://api.postmates.com/{version}/', ['version' => 'v1']],
'defaults' => [
// HTTP Basic auth header, username is api key, password is blank
'auth' => [API_KEY, ''],
]]);
// Create the request
$oRq = $oClient->createRequest(
'POST',
"customers/" . CUST_ID . "/delivery_quotes",
[ 'body' =>
['pickup_address' => '232 E Manhattan Ave, Denver, CO 80203',
'dropoff_address' => '4400 Midwest St, Denver, CO 80205']]);
// Send the request
$oRsp = $oClient->send($oRq);
我正在尝试使用 Guzzle 来使用 RESTful API. Reading the Guzzle documentation,我调用 get
和 post
等方法来配置请求,然后 send
实际上 运行 一个 HTTP 请求。
$request = $client->post('http://httpbin.org/post', array(), array(
'custom_field' => 'my custom value',
'file_field' => '@/path/to/file.xml'
));
$response = $request->send();
但是在我的例子中,似乎在调用 post
期间发送了一个没有字段的 HTTP 请求。这是我的代码
define('CUST_ID', 'blahblah');
define('API_KEY', 'verysecure');
// Construct the underlying Guzzle client
$oClient = new \GuzzleHttp\Client(
['base_url' =>
['http://api.postmates.com/{version}/', ['version' => 'v1']],
'defaults' => [
// HTTP Basic auth header, username is api key, password is blank
'auth' => [API_KEY, ''],
]]);
$oRq = $oClient->post(
"customers/" . CUST_ID . "/delivery_quotes",
[],
['pickup_address' => '232 E Manhattan Ave, Denver, CO 80203',
'dropoff_address' => '4400 Midwest St, Denver, CO 80205']);
发出的请求(没有 POST 参数)
POST /v1/customers/blahblah/delivery_quotes HTTP/1.1
Host: api.postmates.com
Authorization: Basic verysecure==
User-Agent: Guzzle/5.2.0 curl/7.37.1 PHP/5.6.0
Content-Length: 0
和回应
HTTP/1.1 400 BAD REQUEST
Content-Type: application/json
Date: Fri, 13 Feb 2015 07:32:16 GMT
Server: nginx/1.1.19
Content-Length: 205
Connection: keep-alive
{"kind": "error", "code": "invalid_params", "params": {"dropoff_address": "This field is required.", "pickup_address": "This field is required."}, "message": "The parameters of your request were invalid."}
所以看起来我正在验证,但没有传递任何 post 参数。我尝试使用 setPostField
方法设置 POST 字段,但没有成功。正如我所说,在那种情况下,似乎在调用 setPostField
之前发送了一个 HTTP 请求,在任何一种情况下,在我调用 send
.
我试过 guzzlehttp/guzzle 5.2.0 和 5.0.0。
哇,看起来我在看 2 份文档,一份用于 the newer GuzzleHttp\Client
, one for the older Guzzle\Http\Client
。与新库的最大区别在于,如果您想延迟调用 HTTP 请求,则需要调用 createRequest
。
新库下的工作代码
define('CUST_ID', 'blahblah');
define('API_KEY', 'verysecure');
// Construct the underlying Guzzle client
$oClient = new GuzzleHttp\Client(
['base_url' =>
['http://api.postmates.com/{version}/', ['version' => 'v1']],
'defaults' => [
// HTTP Basic auth header, username is api key, password is blank
'auth' => [API_KEY, ''],
]]);
// Create the request
$oRq = $oClient->createRequest(
'POST',
"customers/" . CUST_ID . "/delivery_quotes",
[ 'body' =>
['pickup_address' => '232 E Manhattan Ave, Denver, CO 80203',
'dropoff_address' => '4400 Midwest St, Denver, CO 80205']]);
// Send the request
$oRsp = $oClient->send($oRq);