Page 1 of 1

fsockets open Error 110 Connection timed out

Posted: Tue Sep 19, 2006 1:41 pm
by loveccb
Hello,

I'm attempting a GET request and I'm receiving a timeout. Strange thing is I've got a DEV url and a PROD url. And only the DEV url is working. I'd rather not list the urls here since these are live urls. Any idea why one url might be working the other isn't. The client claims there is no difference b/t the servers. I believe their server is IIS.

Many thanks. The code is below for reference.

Code: Select all

$url = preg_replace("@^https://@i", "", $url); //https
  $url = preg_replace("@^http://@i", "", $url);  //or http
  $host = substr($url, 0, strpos($url, "/"));
  $uri = strstr($url, "/");

      //BUILD POST BODY
      $reqbody = "";
      foreach($datastream as $key => $val) {
          if (!empty($reqbody))
           $reqbody.= "&";
           $reqbody.= $key."=".urlencode($val);
        }

       //BUILD POST HEADER
      $contentlength = strlen($reqbody);

  //$debug = true;
  $debug = false;

  if (!$debug) {
        $reqheader =  "GET $uri HTTP/1.0\r\n".
        "Host: $host\r\n". "User-Agent: PostIt\r\n".
        "Content-Type: text/xml\r\n".
        "Content-Length: $contentlength\r\n\r\n".
        "$reqbody\r\n";
  } else {
        $reqheader =  "GET $uri?" . $reqbody . " HTTP/1.0\r\n".
        "Host: $host\r\n". "User-Agent: PostIt\r\n".
        "Content-Type: text/plain\r\n\r\n";
  }

      //Connect to the Datasource
      $socket = fsockopen($host, 80, $errno, $errstr);

      if (!$socket) {
         $err = trigger_error("ERROR", E_USER_WARNING);
         $result["errno"] = $errno;
         $result["errstr"] = $errstr;
         writeERRtoDB($errno);
         return $result;

      }

      fputs($socket, $reqheader);

      while (!feof($socket)) {
         $result[] = fgets($socket, 4096);
      }
      fclose($socket);

  Print_r ($result);

Posted: Tue Sep 19, 2006 4:07 pm
by Mordred

Code: Select all

if (!$debug) { 
        $reqheader =  "POST $uri HTTP/1.0\r\n".

Posted: Tue Sep 19, 2006 4:20 pm
by loveccb
No, I actually need to do a get.
I was finally successful and I post my solution here in case anyone else runs/ran into the same problem.

I am posting to a https: directory so I need to use "ssl://" in fsocketopen

My connect code:

Code: Select all

$socket = fsockopen(ssl://www.examplepage.php, 443, $errno, $errstr);
Thanks for the reply Mordred!

Posted: Tue Sep 19, 2006 4:33 pm
by Mordred
Read again your HTTP RFC, esp. the sections about GET and POST.
If you need GET, use the code on the 'else' part of the if (!$debug) condition.
You need a request body only when POST-ing (although I'm not completely sure if having a request body with GET is a protocol violation)