在 Guzzle 中发出请求时,您可以使用它来设置用户名和密码:
$request->setAuth($username, $password);
这对我的单个请求非常有用。现在我需要发出一组并行请求,但我不知道应该如何设置它。我已经为我的单个请求和并行请求设置auth
了我的标头,就像我说的那样,单个可以工作,因为我可以设置. 如何设置并行请求,或者我需要使用其他东西吗?Authorization
$request->setAuth()
setAuth()
编辑这是我的代码。
$client = new Client('https://service.example.com/');
$username = 'username';
$password = 'password';
$auth = base64_encode($username . ":" . $password);
$dm = $_POST['domains'];
$domains = explode("\n", $dm);
$client->setDefaultOption('auth', array($username, $password, 'Any'));
$options = array(
'query' => array(
'unit'=> 'y',
'period' => $_POST['period'],
'password' => urlencode($_POST['password']),
'registrant' => 'abcdefg-00004'
),
'headers' => array(
'Authorization' => 'Basic '.$auth,
'Accept' =>'application/hal+json',
'Access-Control-Allow-Origin' => '*'
),
'auth' => array($username, $password, 'Basic'),
'timeout' => '30',
'connect_timeout' => '10',
'cookies' => array('cert_url' => 'https://url.tocert.com/cert/su82jhdea3df5e81cd2cs8ejrae621b3a475312vd'),
'verify' => true,
'debug' => true,
);
$requests = array();
foreach($domains as $d){
$requests[] = $client->post($_POST['server-name'].'/domain?method=POST&callback=?&name='.$d, array(), $options);
}
//Do it
try {
$bulk_create = $client->send($requests);
foreach($bulk_create as $create){
echo $create->getBody();
}
} catch (MultiTransferException $e) {
echo "The following exceptions were encountered:\n";
foreach ($e as $exception) {
echo $exception->getMessage() . "\n";
}
echo "The following requests failed:\n";
foreach ($e->getFailedRequests() as $request) {
echo $request . "\n\n";
}
echo "The following requests succeeded:\n";
foreach ($e->getSuccessfulRequests() as $request) {
echo $request . "\n\n";
}
}