Jump to content

[SOLVED] fsockopen


tinker

Recommended Posts

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).

Link to comment
Share on other sites

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");

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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... :o

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.