Page 1 of 1

"failed to parse address" error..please help

Posted: Tue Oct 06, 2009 6:57 am
by tinoda
after I run my code I get another error on this function below "failed to parse address error" . Is my snytax okay or is there anything that u can spot in my code that needs changing. Thank you for help.

Code: Select all

function sendRequest ($request,$host,$port) {
    // +++++++ connection //
    $socket = fsockopen($host, $port, $errno, $errstr);
    if (!$socket) {
        die("<br>$errno - $errstr<br>");
    }
    // +++++++ Request  //
    $httpRequestLine = "post /xml http/1.1\n";
    $httpHeader = "content-length: ";
    $contentLength = strlen($request);
    $httpRequest = $httpRequestLine . $httpHeader . $contentLength . "\n\n" . $request;
    fputs($socket, $httpRequest, strlen($httpRequest));
    // +++++++ read answer //
    $response = "";
    $line = "";
    while (1) {
          $character = fgetc($socket);
          if ($character == "\n") {
              break;
          } else {
              $line .= $character;
          }
    }
    $requestLineElements = explode(" ",$line);
    if (in_array("200",$requestLineElements)) {
        $line = "";
        while (1) {
               $character = fgetc($socket);
               if ($character == "\n") {
                  break;
               } else {
                  $line .= $character;
               }
        }
        $contentHeaderLine = explode(" ",$line);
        $contentLength = $contentHeaderLine[1];
        if ($contentLength > 0) {
            $bytesRead = 0;
            $line = "";
            while (1) {
                  $character = fgetc($socket);
                  if ($character == "\n") {
                      break;
                  } else {
                      $line .= $character;
                  }
            }
            while ($bytesRead < $contentLength) {
                   $c =  fgetc($socket);
                   $response .= $c;
                   $bytesRead = strlen($response);
            }
        }
    } else {
            $response = $line;
    }
    // +++++++ close connection //
    fclose($socket);
    return $response;
}
echo sendRequest();