http请求不成功

snipersheep 2013-06-24 11:06:36
下面代码为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>
<OEMID>0</OEMID>
<UserName>test07</UserName>
<Password>123456</Password>
<ConfirmPassword>123456</ConfirmPassword>
<EMailBox>shengxiumei@fkhl.sh.cn</EMailBox>
<Name></Name>
<DevAttr></DevAttr>
</WSRequest>
</ns1:UserOperation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>";

// soap 发送
// 第一次http请求
$ret_str = Socket_Connect_HTTP_Post("www.liveipc.com", "80", "/ipcam/soapservice", "", $soap_data); //数据包
// 第二次http请求
if($ret_str) {
$result = Socket_Connect_HTTP_Post("www.liveipc.com", "80", "/ipcam/soapservice", "", $soap_data, 200);
}
...全文
222 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lumengabc 2013-06-24
  • 打赏
  • 举报
回复
错误代码有提示么? 这堆代码可以用编辑器框选起来,不然太乱。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧