Php guzzle HTTP/guzzle在之前发送HTTP请求->;发送()
我试着用大口大口喝一杯。阅读,我调用Php guzzle HTTP/guzzle在之前发送HTTP请求->;发送(),php,rest,composer-php,guzzle,postmates,Php,Rest,Composer Php,Guzzle,Postmates,我试着用大口大口喝一杯。阅读,我调用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
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参数)
以及回应
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字段,但运气不佳。正如我所说,在这种情况下,HTTP请求似乎是在调用setPostField
之前发送的,在这两种情况下,HTTP请求都是在调用send
之前发送的
我试过guzzle http/guzzle 5.2.0和5.0.0。哇,看起来我在看两份文档,一份用于,一份用于。新库的最大区别在于,如果要延迟HTTP请求的调用,则需要调用
createRequest
$request = $client->post('http://httpbin.org/post', array(), array(
'custom_field' => 'my custom value',
'file_field' => '@/path/to/file.xml'
));
$response = $request->send();
新库下的工作代码
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);
哇,看起来我在看两份文档,一份用于,一份用于。新库的最大区别在于,如果要延迟HTTP请求的调用,则需要调用
createRequest
$request = $client->post('http://httpbin.org/post', array(), array(
'custom_field' => 'my custom value',
'file_field' => '@/path/to/file.xml'
));
$response = $request->send();
新库下的工作代码
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);