(curl php)使用post方法维护http会话?

时间:2014-05-05 04:45:01

标签: php curl http-post session-variables

这是我在Stackoverflow中的第一篇文章。目前我找到了一份新工作,这是一项简单的任务,但是作为一个新手,它更难以理解。

我不想做http post请求,有些代码如下:

POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: "Notification"
User-Agent: Jakarta Commons-HttpClient/3.1
Host: sample.com:8001
Content-Length: 201

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<LGI>
<OPNAME>user</OPNAME>
<PWD>password</PWD>
</LGI>
</soapenv:Body>
</soapenv:Envelope>

这是php代码:

$soapUrl = "http://sample.com:8001";
$cookiefile = '/tmp/curl-session';
$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<LGI>
<OPNAME>user</OPNAME>
<PWD>pass</PWD>
</LGI>
</soapenv:Body>
</soapenv:Envelope>
';
echo($xml_post_string."\n");

$headers = array(
"POST / HTTP/1.1",
"Content-Type: text/xml;charset=UTF-8",
"SOAPAction: \"Notification\"",
"User-Agent: Jakarta Commons-HttpClient/3.1",
"Host: sample.com:8001",
"Content-Length: ".strlen($xml_post_string)
); 

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch); 

echo("\n======================== login result ================================\n");

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
echo("header:\n".$header."\n");
echo("body:\n".$body."\n");
//============= parsing header =====================
$id_t=explode("http://sample.com:8001",$header);
$id=$id_t[1];
echo("ID POST=".$id."\n");

从那里我得到一个会话ID,我想发送会话维护,如:

POST /00500000 HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: "Notification"
User-Agent: Jakarta Commons-HttpClient/3.1
Host: sample.com:8001
Content-Length: 0

其中/00500000是会话ID。 如何创建php来维护会话?

以下是维护会话的其余代码但失败但错误为400, bad request

//============= handshake session ==================
$xml_post_string = '';

$headers = array(
"Content-Type: text/xml;charset=UTF-8",
"SOAPAction: \"Notification\"",
"User-Agent: Jakarta Commons-HttpClient/3.1",
"Host: sample.com:8001",
"Content-Length: ".strlen($xml_post_string)
);
print_r($headers."\n");
$post = "POST ".$id." HTTP/1.1";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch); 

echo("\n======================== handshake result ================================\n");
//echo($response."\n");
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$cek = explode("Content-Length: 0",$header);
$c_cek = count($cek);
if ($c_cek > 1) {
$body = substr($response, $header_size);
echo("header:\n".$header."\n");
echo("body:\n".$body."\n");
}
else {
$body = substr($response, $header_size);
echo("header:\n".$header."\n");
echo("body:\n".$body."\n");
}

0 个答案:

没有答案