下面代码为HTTP请求代码。
第二次请求不成功,请帮忙看一下错在哪里?
正常流程:发送http请求,401响应,再次发送带验证的http请求,返回200。结束。
注:代码中的账号密码都是默认的,所以没有提取第一次返回的数据中的账号,密码。第二次请求中的账号密码都用默认数据。
function Socket_Connect_HTTP_Post($server, $port, $dir, $file, $data, $type = '') {
$send_url = str_replace("%2F", "/", rawurlencode(rawurldecode(urldecode($dir . $file))));
$method = "POST";
$http_header_array["ALL"][] = "Accept: */*";
$http_header_array["ALL"][] = "Referer: http://".$server."/";
$http_header_array["ALL"][] = "Accept-Language: en-us,zh-cn";
$http_header_array["ALL"][] = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
$http_header_array["ALL"][] = "Connection: Keep-Alive";
$http_header_array["ALL"][] = "Host: ".$server;
$http_header_array["ALL"][] = "urn:po-processor";
if(!empty($type)) {
$http_header_array["ALL"][] = "Authorization: Digest realm=\"www.fkhl.sh.cn\", username=\"FHL3301009\", nonce=\"MTM3MTc5Njg3Mjg3NjpkN2RmYmE4ZDFmMzNiZGQ0ZTAxMDJjMTNjZjI3MDQ2ZQ==\", uri=\"/ipcam/soapservice\", nc=00000001, cnonce=\"51c3f331ca5600006784\", response=\"dc0f6160daa71c7e86416e0f1cb63d59\", qop=\"auth\"";
}
$http_header_array["ALL"][] = "SOAPAction:http://www.liveipc.com/UserService/UserOperation";
$http_header_array["POST"][] = "Cache-Control: no-cache";
$http_header_array["POST"][] = "Content-Type: text/xml";
$http_header_array["POST"][] = "Content-Length: ".strlen($data);
if (is_array($http_header_array["ALL"])) {
$http_header_str_all = implode("\r\n", $http_header_array["ALL"]);
}
if (is_array($http_header_array[$method])) {
$http_header_str_all .= "\r\n".implode("\r\n", $http_header_array[$method]);
}
$send_all = "POST ".$send_url." HTTP/1.1\r\n".$http_header_str_all."\r\n\r\n";
$send_all .= $data."\r\n\r\n";
$fp_send = fsockopen($server, $port, $errno, $errstr ,30);
if ($fp_send)
{
$can_rcv = 0;
fputs($fp_send, $send_all);
stream_set_timeout($fp_send, 120);
while(!feof($fp_send))
{
$content_t = fgets($fp_send, 1024);
if ((trim($content_t) == "") && !$can_rcv)
{
$can_rcv = 1;
$content_t = fgets($fp_send, 1024);
}
if ($can_rcv == 1 and strlen($content_t)>5)
{
$reply_str .= $content_t;
}
}
$stream_status = stream_get_meta_data($fp_send);
fclose($fp_send);
if ($stream_status[timed_out])
{
$reply_str = "";
}
}
return $reply_str;
}
$soap_data = "<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns1=\"http://www.liveipc.com/UserService/\">
<SOAP-ENV:Body>
<ns1:UserOperation>
<WSRequest>
<Version>1.0.0</Version>
<CharSet>GBK</CharSet>
<TransactionName>UserRegistration</TransactionName>
<Random>c7c92fc2882f41f92249fac90aad7bb6</Random>
<SoapConfigData></SoapConfigData>