fsockets open Error 110 Connection timed out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
loveccb
Forum Newbie
Posts: 10
Joined: Wed Jul 05, 2006 6:48 pm

fsockets open Error 110 Connection timed out

Post 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);
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Code: Select all

if (!$debug) { 
        $reqheader =  "POST $uri HTTP/1.0\r\n".
loveccb
Forum Newbie
Posts: 10
Joined: Wed Jul 05, 2006 6:48 pm

Post 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!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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)
Post Reply