{"id":250,"date":"2011-09-28T16:51:11","date_gmt":"2011-09-28T11:21:11","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=250"},"modified":"2011-09-28T16:51:11","modified_gmt":"2011-09-28T11:21:11","slug":"php-set-socket-time-out-for-fsockopen-set-time-out-for-fsockopen","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/php-socket\/php-set-socket-time-out-for-fsockopen-set-time-out-for-fsockopen\/","title":{"rendered":"php set socket time out for fsockopen, set time out for fsockopen"},"content":{"rendered":"<p>Sometimes if a target host is unreachable by a socket connection it takes 60 seconds to know that the socket function cannot connect to the destination. This is when i am using fsockopen function to connect to a remote host. So i was wondering there should be a function to timeout according to our preference and found that stream_set_timeout could do that.<\/p>\n<pre lang='php'>\r\n\r\n$fp = fsockopen(\"www.example.com\", 80);\r\nif (!$fp) {\r\n    echo \"Unable to open\\n\";\r\n} else {\r\n\r\n    fwrite($fp, \"GET \/ HTTP\/1.0\\r\\n\\r\\n\");\r\n    stream_set_timeout($fp, 2);\r\n    $res = fread($fp, 2000);\r\n\r\n    $info = stream_get_meta_data($fp);\r\n    fclose($fp);\r\n\r\n    if ($info['timed_out']) {\r\n        echo 'Connection timed out!';\r\n    } else {\r\n        echo $res;\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\n<p>where the second parameter of stream_set_timeout is in seconds.<\/p>\n<p>&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes if a target host is unreachable by a socket connection it takes 60 seconds to know that the socket function cannot connect to the destination. This is when i am using fsockopen function to connect to a remote host. So i was wondering there should be a function to timeout according to our preference [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[30,31,29],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/250"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":1,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":251,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/250\/revisions\/251"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}