tinker Posted January 12, 2008 Share Posted January 12, 2008 I'm having a play with fsockopen, just trying to get a web page really. However both on my local machine and my host, no matter what page I try I get a 404 error, unless I just request root (/index.html) which returns the found apache page, even though there is a page named index.html. Any ideas? $msg = "GET /tester/socks/index.html HTTP/1.0\r\n"; $msg .= "Connection: Close\r\n\r\n"; P.S. my aim is to replace the use of an actual form for paypal (and I know very similar code to this works when communicating with paypal, on this host). Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/ Share on other sites More sharing options...
Ken2k7 Posted January 12, 2008 Share Posted January 12, 2008 Where's the fsockopen part of your code? Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437542 Share on other sites More sharing options...
trq Posted January 12, 2008 Share Posted January 12, 2008 Can we see some relevent code? Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437543 Share on other sites More sharing options...
tinker Posted January 12, 2008 Author Share Posted January 12, 2008 I'm assuming it's to do with the actual request, ive tried HTTP/1.0 & HTTP/1.1, plus a few other variations. I can do the same using telnet and it works fine, so i'm at a loss... function opensock($addr, $port, $msg, $timeout=30) { $sret = ""; $fp = fsockopen ($addr, $port, $errno, $errstr, $timeout); if (!$fp) { echo "$errstr ($errno)<br/>\n"; echo $fp; return -1; } else { fputs ($fp, $msg); while (!feof($fp)) { $sret .= fgets ($fp, 1024); } fclose ($fp); } return $sret; } $addr = "www.somewhere.co.uk"; $port = 80; $msg = "GET /tester/socks/index.html HTTP/1.0\r\n"; $msg .= "Connection: Close\r\n\r\n"; $ret = opensock($addr, $port, $msg, 5); print $ret."<br>"; This is how I generated the a form msg before the first tests: function gen_msg($msg) { $sret = ""; $sret .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; $sret .= "Content-Type: application/x-www-form-urlencoded\r\n"; $sret .= "Content-Length: ".strlen($msg)."\r\n\r\n"; $sret .= $msg; return $sret; } $msg = gen_msg("submit=true&cmd=test"); Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437549 Share on other sites More sharing options...
Ken2k7 Posted January 12, 2008 Share Posted January 12, 2008 $fp = fsockopen ($addr, $port, $errno, $errstr, $timeout); Just a quick question: are all those variables defined? ($errno and $errstr) Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437551 Share on other sites More sharing options...
trq Posted January 12, 2008 Share Posted January 12, 2008 You need to use a complete url as the address. Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437553 Share on other sites More sharing options...
tinker Posted January 12, 2008 Author Share Posted January 12, 2008 if I use a full url then I get the following error (both local and host). However the paypal script doesn't use a full url and neither does the official php docs? Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (12938024) Defining the error vars makes no diff... I didn't bother assuming could inline because no other code i've seen does either??? Me no kno fsockopen Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437558 Share on other sites More sharing options...
trq Posted January 12, 2008 Share Posted January 12, 2008 Sounds like you don't have url wrappers enabled in your php.ini. Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437577 Share on other sites More sharing options...
tinker Posted January 12, 2008 Author Share Posted January 12, 2008 I thought this, but because paypal didn't need to use them to send a form I assume that it's possible without. So that's why i'm proceeding this way and not that. For some reason it's now working on my localhost but I can't figure out what i've changed, it sends a dummy form to the other page, processes it and responds appropriately. But my host's version fails as before, even though the paypal verify script works fine? Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437582 Share on other sites More sharing options...
tinker Posted January 13, 2008 Author Share Posted January 13, 2008 After reading some obscure article about 404's and forms, I was lead to reading section 14.23 of rfc2616, which goes like this: 14.23 Host The Host request-header field specifies the Internet host and port number of the resource being requested, as obtained from the original URI given by the user or referring resource (generally an HTTP URL, as described in section 3.2.2). The Host field value MUST represent the naming authority of the origin server or gateway given by the original URL. This allows the origin server or gateway to differentiate between internally-ambiguous URLs, such as the root "/" URL of a server for multiple host names on a single IP address. Host = "Host" ":" host [ ":" port ] ; Section 3.2.2 A "host" without any trailing port information implies the default port for the service requested (e.g., "80" for an HTTP URL). For example, a request on the origin server for <http://www.w3.org/pub/WWW/> would properly include: GET /pub/WWW/ HTTP/1.1 Host: www.w3.org A client MUST include a Host header field in all HTTP/1.1 request messages . If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value. An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field. See sections 5.2 and 19.6.1.1 for other requirements relating to Host. This inspired me to amend my message generation function as follows: function gen_msg($msg, $page, $host) { $sret = ""; $sret .= "POST ".$page." HTTP/1.1\r\n"; $sret .= "Host: ".$host."\r\n"; $sret .= "Content-Type: application/x-www-form-urlencoded\r\n"; //$sret .= "Content-Type: text/html; charset=utf-8\r\n"; $sret .= "Content-Length: ".strlen($msg)."\r\n\r\n"; $sret .= $msg."\r\n"; //$sret .= "Connection: Close\r\n\r\n"; return $sret; } However, I realise now I actually need to redirect my user's to said page, so this isn't my solution, but it is topic solved... Quote Link to comment https://forums.phpfreaks.com/topic/85734-solved-fsockopen/#findComment-437851 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.