ばかげた問題が発生しています。特定のバイト数を取得してから、接続を閉じる必要があります。ヘッダーから「コンテンツの長さ」を抽出し、その長さを使用して、接続を切断する前に取得する必要のあるバイト数を決定できることがわかりました。しかし、私は間違いを犯したようです。(本来あるべき)2428ではなく、40バイトしか取得していません。おそらく私はfgets()を正しく使用していませんか?
コードは次のとおりです。
private static function Send($URL, $Data) {
$server = parse_url($URL, PHP_URL_HOST);
$port = parse_url($URL, PHP_URL_PORT);
// If the parsing the port information fails, we will assume it's on a default port.
// As such, we'll set the port in the switch below.
if($port == null) {
switch(parse_url($URL, PHP_URL_SCHEME)) {
case "HTTP":
$port = 80;
break;
case "HTTPS":
$port = 443;
break;
}
}
// Check if we are using a proxy (debug configuration typically).
if(\HTTP\HTTPRequests::ProxyEnabled) {
$server = \HTTP\HTTPRequests::ProxyServer;
$port = \HTTP\HTTPRequests::ProxyPort;
}
// Open a connection to the server.
$connection = fsockopen($server, $port, $errno, $errstr);
if (!$connection) {
die("OMG Ponies!");
}
echo "===========================================================<BR>";
echo "Connection Open<BR>";
echo "The Time is " . date("H:i:su", time()) . "<BR>";
echo "Sending Request<BR>";
fwrite($connection, $Data);
echo "Request Sent.";
echo "The Time is " . date("H:i:su", time()) . "<BR>";
$responseheader = "";
$responsebody = "";
/*
\HTTP\HTTPRequests::$start = NULL;
\HTTP\HTTPRequests::$timeout = 10;
// @todo: Rewrite this. Should keep checking for '/r/n/r/n', then check for a content length header. If found, keep grabbing bytes, then close. If not, then close immediately.
while(!\HTTP\HTTPRequests::safe_feof($connection, \HTTP\HTTPRequests::$start) && (microtime(true) - \HTTP\HTTPRequests::$start) < \HTTP\HTTPRequests::$timeout)
{
$response .= fgets($connection);
}
*/
echo "Getting Response<BR>";
echo "The Time is " . date("H:i:su", time()) . "<BR>";
while(!feof($connection) && !(strlen(strstr($responseheader,"\r\n\r\n"))>0)) {
$responseheader .= fgets($connection);
}
echo "The Header is fully received at " . date("H:i:su", time()) . "<BR>";
echo "Header (raw):" . "<BR>";
echo "/////////////////////////////////////////////<BR>";
echo $responseheader . "<BR>";
echo "/////////////////////////////////////////////<BR>";
if((strlen(strstr($responseheader,"Content-Length:"))>0)) {
$contentlength = ((int)(\Extract\Extract::GetContentLength($responseheader)));
//for($i = 0; $i < $contentlength; $i++) {
$responsebody .= fgets($connection, $contentlength);
//}
echo "The Body is fully received at " . date("H:i:su", time()) . "<BR>";
echo "Body (raw):" . "<BR>";
echo "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}<BR>";
echo $responsebody . "<BR>";
echo "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}<BR>";
echo "<B>!Content length is " . strlen($responsebody) . ". It should be " . $contentlength . "!</B><BR>";
}
echo "Response Received.<BR>";
echo "The Time is " . date("H:i:su", time()) . "<BR>";
fclose($connection);
echo "Connection Closed.<BR>";
echo "The Time is " . date("H:i:su", time()) . "<BR>";
echo "Exiting Send() method.<BR>";
echo "===========================================================<BR>";
echo "<BR>";
return $responseheader . $responsebody;
}
そしてここに出力があります:
===========================================================
Connection Open
The Time is 15:57:29000000
Sending Request
Request Sent.The Time is 15:57:29000000
Getting Response
The Time is 15:57:29000000
The Header is fully received at 15:57:29000000
Header (raw):
/////////////////////////////////////////////
HTTP/1.1 207 Multi-Status Date: Wed, 11 Apr 2012 19:57:18 GMT Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.14 with Suhosin-Patch mod_ssl/2.2.8 OpenSSL/0.9.8g mod_wsgi/2.0 Python/2.5.2 mod_perl/2.0.3 Perl/v5.8.8 X-Powered-By: PHP/5.2.4-2ubuntu5.14 DAV: 1, 2, 3, access-control, calendar-access, calendar-schedule DAV: extended-mkcol, calendar-proxy, bind, addressbook, calendar-auto-schedule Content-Location: /davical/caldav.php/rwr26/home/ ETag: "8378ead7e628deafd91ec99dc180ad74" X-DAViCal-Version: DAViCal/0.9.9; DB/1.2.9 Content-Length: 2428 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/xml; charset="utf-8"
/////////////////////////////////////////////
The Body is fully received at 15:57:29000000
Body (raw):
{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
!Content length is 40. It should be 2428!
Response Received.
The Time is 15:57:29000000
Connection Closed.
The Time is 15:57:29000000
Exiting Send() method.
===========================================================
なぜ私はそれをこのようにしているのですか?どうやら、私はある種のタイムアウトエラーに遭遇していました。これにより、各接続が30秒間開いたままになります(はい、30秒全体)。そこで、正確なバイト数を取得して閉じるためにこのコードを記述しました。どんな助けでも大歓迎です。