M
M
max_mara2011-10-18 05:28:45
PHP
max_mara, 2011-10-18 05:28:45

PHP Socket Proxy Server?

Good afternoon, colleagues,

for the purpose of self-development, I am trying to deal with sockets

. In general, I open a socket, connect it to the server, send the following headers there

GET <a href="http://www.google.com/">www.google.com/</a> HTTP/1.1<br/>
Host: <a href="http://www.google.com">www.google.com</a><br/>
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1<br/>
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<br/>
Accept-Language: en-us,en;q=0.5<br/>
Accept-Encoding: gzip, deflate<br/>
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7<br/>
Proxy-Connection: keep-alive<br/>
Cache-Control: max-age=0<br/>


I receive in response
HTTP/1.1 200 OK<br/>
Date: Tue, 18 Oct 2011 02:01:58 GMT<br/>
Expires: -1<br/>
Cache-Control: private, max-age=0<br/>
Content-Type: text/html; charset=UTF-8<br/>
Content-Encoding: gzip<br/>
Server: gws<br/>
Content-Length: 15101<br/>
X-XSS-Protection: 1; mode=block<br/>
X-Frame-Options: SAMEORIGIN<br/>


Those. getting headers but not getting page content.
I do it like this:
socket_write($this-&gt;_socket, $data, strlen($data));<br/>
return socket_read($this-&gt;_socket, 1024 * 1024);<br/>

$data - These are the headers.

Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kindman, 2011-10-18
@max_mara

Try retrying socket_read() until the socket is closed.
that is, do not immediately do RETURN, but first concatenate the fragments.
It's just that servers sometimes make delays between generating the header and sending the result. Just don't rush!

S
Sergey Beresnev, 2011-10-18
@sectus

All in a bunch, maybe the conclusion itself is somehow not doing it right?

K
Kaydannik, 2011-10-18
@Kaydannik

Error in protocol
GET www.google.com/ HTTP/1.1 - wrong.
That's right - GET / HTTP / 1.1
This specifies a request without a host.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question