我想使用Guzzle 6.0转换所有api调用(之前使用curl_multi)。但是,当我发布帖子请求时,会收到未经授权的回复。
工作:
$url = "http://192.168.101.10/WebConnect/search.asp?";
$request_data = 'CID=test&PWD=test&ORD=;0451103144;;1;;';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
$reply = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
} else {
echo '<pre>';
print_r($reply);
echo '</pre>';
}
curl_close($ch);
使用Guzzle 6.0
$client = new Client();
$res = $client->post($url, [
'timeout' => 30,
'body' => $request_data,
]);
echo $res->getStatusCode();
echo $res->getHeader('content-type');
echo $res->getBody();
错误:
客户端错误:
POST http://192.168.101.10/WebConnect/search.asp?
导致401 Unauthorized
响应:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml (truncated...)
我试过
'auth' => ['test','test'] //doesnot work either
此外,我尝试添加标题,但它不会运行相同的错误
$headers = array(
'Content-type' => 'text/plain;charset="utf-8"',
'Accept' => 'text/plain',
'Content-length' => strlen($test)
);
以及
$headers = array(
'Accept' => 'text/html',
'Content-Type' => 'text/html',
);
并且我也尝试提交单独的参数
$requestBody = array(
'CID' => 'test',
'PWD' => 'test',
'ORD' => ';0451103144;;1;'
);
$request = $client->post($url,
array(
'headers' => $headers,
'form_params' => $requestBody
));
为什么说<html xml (truncated...)
xml .. ??当我特别提到其text/plain
或text/html