scuarplex Posted May 28, 2011 Share Posted May 28, 2011 Hi, i'm developing an app in wampp, i'm usually use cUrl but this time i wanted to try sockets for a change and more portability. I have followed the basic socket structure but i can't connect. So this is my code: function googlear($search,$filtro,$safe) { //Abrimos el socket $socket = fsockopen( "www.google.com", 80, $errno,$errdesc); if (!$socket) {print "$errstr ($errno)<br/>\n";die;}; $busqueda = "search?q=".$search."&num=100";//Solo me va a tirar 100 resultados por ahora, para ir pasando de pagina arranco de start=0 y voy sumando de a 100 para pasar de pagina. if($safe = true) { $busqueda = $busqueda . "&safe=active"; } if($filtro != true) { $busqueda = $busqueda . "&filter=0"; } $request = "GET $busqueda HTTP/1.1\r\n"; $request .= "Host: www.google.com\r\n"; $request .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1\r\n"; $request .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; $request .= "Accept-Language: es-ar,es;q=0.8,en-us;q=0.5,en;q=0.3"; $request .= "Accept-Encoding: gzip, deflate\r\n"; $request .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"; $request .= "Connection: keep-alive\r\n"; //Mando las cabeceras con el socket como resource fputs($socket, $request); while(!feof($socket)) { $busqueda[] = fgets($socket, 1024); } //Cerramos fclose($socket); return count($busqueda); } As you can see i'm doing a GET and waiting for the count of lines of the array as result (just to test), but i'm getting "Fatal error: Maximum execution time of 30 seconds exceeded in [...]". It can't take that long to make a GET Request... Any clues? Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/ Share on other sites More sharing options...
mikesta707 Posted May 28, 2011 Share Posted May 28, 2011 try commenting out certain lines so you can narrow down what is hanging your script up. You can also increase the max execution time setting in your php.ini Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1221665 Share on other sites More sharing options...
btherl Posted May 30, 2011 Share Posted May 30, 2011 Try sending another \r\n after the headers. Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1222083 Share on other sites More sharing options...
scuarplex Posted May 31, 2011 Author Share Posted May 31, 2011 Try sending another \r\n after the headers. It was that, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1222825 Share on other sites More sharing options...
scuarplex Posted June 1, 2011 Author Share Posted June 1, 2011 My bad it wasn't solved, but i think i have discovered the problem, it's just that i don't know how to solve it. I think that my problem is that my script is running on windows and the google server it's not. So the end of the line/file will be different and it won't be recognized. Anyone know how to solve this? Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1223287 Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 \n is a linefeed for non-DOS/Windows machines. Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1223303 Share on other sites More sharing options...
btherl Posted June 1, 2011 Share Posted June 1, 2011 I would try requesting "/search ..." instead of "search ...". IE, add a slash at the start. Even better, use an extension such as Firefox LiveHTTPHeaders to see the exact headers sent on a successful request and copy those. I doubt that line feeds are the problem. What exactly is the response you get from the server? Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1223315 Share on other sites More sharing options...
scuarplex Posted June 1, 2011 Author Share Posted June 1, 2011 btherl: I did what you told me, and i put the "/" in search and i got exactly the same headers but still i'm getting: "Fatal error: Maximum execution time of 30 seconds exceeded in ..." I've been using wireshark (a sniffer) while i was using this function to see if i was connecting, and this is what i get: It seems like the socket it's connecting, but i still got that error... PS: changed the socket to run on ssl with ssl://encrypted.google.com and 443 as port from the original. Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1223650 Share on other sites More sharing options...
btherl Posted June 2, 2011 Share Posted June 2, 2011 That wireshark log from an SSL session isn't telling me much. Can you log an unencrypted connection instead? An exact dump of what's being sent would be very helpful (there's an option somewhere to "follow" a tcp session and it'll show you exactly what went back and forth). Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1223932 Share on other sites More sharing options...
scuarplex Posted June 2, 2011 Author Share Posted June 2, 2011 That wireshark log from an SSL session isn't telling me much. Can you log an unencrypted connection instead? An exact dump of what's being sent would be very helpful (there's an option somewhere to "follow" a tcp session and it'll show you exactly what went back and forth). Hi mate, i've been trying to connect to www.google.com on port 80 to keep it simple for now, and i do what you've told me and i've found that i'm getting "400 Bad Request" as response. Also, look this picture: http://s2.postimage.org/n5vk1quoy/bacdchk.jpg As you can notice there's some kind of bad checksum error from my ip to google's IP, maybe that it's provoking the bad request? In the attachment is the capture between my pc and google, and this is the code: function googlear($search,$filtro,$safe) { $search = str_replace(" ","+",$search); //Abrimos el socket $socket = fsockopen( "64.233.163.104", 80, $errno,$errdesc,30); if (!$socket) {print "$errstr ($errno)<br/>\n";die;}; $busqueda = "/search?q=".$search; if($safe = true) { $busqueda = $busqueda . "&safe=active"; } if($filtro != true) { $busqueda = $busqueda . "&filter=0"; } $request = "GET $busqueda HTTP/1.1\r\n"; $request .= "Host: www.google.com/\r\n"; $request .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1\r\n"; $request .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; $request .= "Accept-Language: es-ar,es;q=0.8,en-us;q=0.5,en;q=0.3"; $request .= "Accept-Encoding: gzip, deflate\r\n"; $request .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"; $request .= "Keep-Alive: 115\r\n"; $request .= "DNT: 1\r\n"; $request .= "Connection: keep-alive\r\n\r\n"; $get = array(); fputs($socket, $request); //stream_set_blocking($socket, 0); while(!feof($socket)) { $get[] = fgets($socket, 1024); } //Cerramos fclose($socket); return count($get); } Greets, and thanks for your answers [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1224281 Share on other sites More sharing options...
btherl Posted June 3, 2011 Share Posted June 3, 2011 Thanks for that dump. I doubt the TCP checksums are an issue, those are calculated by your OS and will not be wrong. Those will only be an issue if you are sending data at the "raw socket" level, but what you're using here is a "cooked socket", where the OS takes care of TCP and IP checksums. So the issue will be with the HTTP protocol - Google is saying your request doesn't make sense. I can see one issue here: $request .= "Accept-Language: es-ar,es;q=0.8,en-us;q=0.5,en;q=0.3"; There is no \r\n on this line. So google will be interpreting the following header line as a continuation of this one. I don't know if that's enough to cause a "400 Bad Request" response though. What I would try next if I was doing this is to remove all the headers except the "Host:" header and see what happens. Ie just this: $request = "GET $busqueda HTTP/1.1\r\n"; $request .= "Host: www.google.com/\r\n"; $request .= "\r\n"; # Put this on a seperate line so it doesn't get lost when re-ordering and adding/removing headers Now that is valid HTTP/1.0 for sure, I don't know if it's valid HTTP/1.1, but I think it would be. Also regarding your capture, it didn't show the full text that you sent. If need to make another capture, see if you can "zoom" into the packet where your pc sends the HTTP request to google, and get the full contents of it. Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1224452 Share on other sites More sharing options...
scuarplex Posted June 3, 2011 Author Share Posted June 3, 2011 Thanks for that dump. I doubt the TCP checksums are an issue, those are calculated by your OS and will not be wrong. Those will only be an issue if you are sending data at the "raw socket" level, but what you're using here is a "cooked socket", where the OS takes care of TCP and IP checksums. So the issue will be with the HTTP protocol - Google is saying your request doesn't make sense. Aaa ok, thanks for the tip I can see one issue here: $request .= "Accept-Language: es-ar,es;q=0.8,en-us;q=0.5,en;q=0.3"; There is no \r\n on this line. So google will be interpreting the following header line as a continuation of this one. I don't know if that's enough to cause a "400 Bad Request" response though. Oops, i missed that one. I just tried with the \r\n at the end of the line but i'm still getting 400. What I would try next if I was doing this is to remove all the headers except the "Host:" header and see what happens. Ie just this: $request = "GET $busqueda HTTP/1.1\r\n"; $request .= "Host: www.google.com/\r\n"; $request .= "\r\n"; # Put this on a seperate line so it doesn't get lost when re-ordering and adding/removing headers Now that is valid HTTP/1.0 for sure, I don't know if it's valid HTTP/1.1, but I think it would be. Also regarding your capture, it didn't show the full text that you sent. If need to make another capture, see if you can "zoom" into the packet where your pc sends the HTTP request to google, and get the full contents of it. I just changed the headers to Get,Host,and jump line like you said but i still got squat. The only strange thing that i notice was when i change the protocol with this headers to HTTP/1.0. I still got the 400 error but this time i got the google Bad request web source instead of a php error. I'll attach the expanded capture, but it's a bit tedious. One more thing: I checked the packet when i was sending the GET trough google website and the headers are exactly the same that i write the second time (with the \r\n near Accept-Language ) (I'm using wamp and i aready make sure that the extension is enable on every php.ini there is on my computer). Greets [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1224472 Share on other sites More sharing options...
btherl Posted June 5, 2011 Share Posted June 5, 2011 Here's a LiveHTTPHeaders dump of a valid request (with a few sensitive headers removed): GET /search?q=http%3A%2F%2Fmailtrackpro.com%2Findex.php%2Fbottom-menu%2Fnuestros-clientes%2F&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a HTTP/1.1 Host: www.google.com User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive Now most of that is unimportant, but what I do notice is your search term contains some characters that should be url encoded. Firefox (which I made this request) has done that encoding, which is why you see %3A and %2F in the search request. So I would try modifying your code like this: $busqueda = "/search?q=".urlencode($search); If you look at the wireshark log from your connection: 0030 40 3d a7 58 00 00 47 45 54 20 2f 73 65 61 72 63 @=.X..GET /searc 0040 68 3f 71 3d 68 74 74 70 3a 2f 2f 6d 61 69 6c 74 h?q=http://mailt 0050 72 61 63 6b 70 72 6f 2e 63 6f 6d 2f 69 6e 64 65 rackpro.com/inde 0060 78 2e 70 68 70 2f 62 6f 74 74 6f 6d 2d 6d 65 6e x.php/bottom-men 0070 75 2f 6e 75 65 73 74 72 6f 73 2d 63 6c 69 65 6e u/nuestros-clien 0080 74 65 73 2f 26 73 61 66 65 3d 61 63 74 69 76 65 tes/&safe=active 0090 26 66 69 6c 74 65 72 3d 30 20 48 54 54 50 2f 31 &filter=0 HTTP/1 00a0 2e 31 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 67 6f .1..Host: www.go 00b0 6f 67 6c 65 2e 63 6f 6d 2f 0d 0a 55 73 65 72 2d ogle.com/..User- That is raw - the url encoding wasn't applied. Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1225671 Share on other sites More sharing options...
scuarplex Posted June 29, 2011 Author Share Posted June 29, 2011 It didn't work either Now the request are exactly the same as the browser request but i still got 400 on my script. I don't know wtf is going on. Anyone could do a simple socket connection script with google to see if anyone can make it? PS: sorry i didn't answer earlier, and thanks btherl for all your help Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1236150 Share on other sites More sharing options...
btherl Posted June 29, 2011 Share Posted June 29, 2011 Can you post a dump of the request you are currently making? Quote Link to comment https://forums.phpfreaks.com/topic/237722-cant-connect-socket-time-out/#findComment-1236158 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.