Click to See Complete Forum and Search --> : PHP sockets - expert help perhaps needed.
Pouncer
Apr 1st, 2007, 01:08 PM
Hey guys, I'm not too familiar with php sockets myself. I was hoping some expert here could aid me.
What I'm wanting to do is connect to http://onelook.com on port 80 and submit a word to lookup.
I'm not sure how I go about setting the socket up, and what to send to the socket etc.
Any ideas guys?
penagate
Apr 1st, 2007, 01:13 PM
That's just a GET request. You can use file_get_contents() (assuming URL wrappers are enabled), and construct the URL manually.
Or is using sockets a requirement?
Pouncer
Apr 1st, 2007, 03:12 PM
Thank you so much for your reply penegate (I posted this problem on many other php forums an no-one replied, vbforums was my last resort)
Well, I already had a attempt at this. But it just keeps giving me a 302 response :s. Using sockets is not a requirement, but I would very much like to use sockets since I like to explore stuff.
<?php
$host = "www.onelook.com";
$fp = fsockopen($host, 80, $errno, $errdesc) or die("Connection to $host failed");
$request = "GET /w=cat&ls=a HTTP/1.1\r\n";
$request .= "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
$request .= "Referer: http://www.onelook.com/?w=cat&ls=a\r\n";
$request .= "Accept-Language: en-us\r\n";
$request .= "Accept-Encoding: gzip, deflate\r\n";
$request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\r\n";
$request .= "Host: www.onelook.com\r\n";
$request .= "Connection: Keep-Alive\r\n\r\n";
fputs($fp, $request);
while(!feof($fp)){
$page[] = fgets($fp, 1024);
}
fclose($fp);
for($i=0; $i<count($page); $i++){
echo $page[$i] . "\n";
}
?>
Any ideas matey? I hope you are the one who can help me!
TheBigB
Apr 1st, 2007, 03:21 PM
10.3.3 302 Found
The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.
The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).
If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
Note: RFC 1945 and RFC 2068 specify that the client is not allowed
to change the method on the redirected request. However, most
existing user agent implementations treat 302 as if it were a 303
response, performing a GET on the Location field-value regardless
of the original request method. The status codes 303 and 307 have
been added for servers that wish to make unambiguously clear which
kind of reaction is expected of the client.
I can't get much from analyzing your code to this response code,
but maybe you could change the referrer just to: http://www.onelook.com/
One other thing that might be possible, is that you need to send a '200 OK' response
Pouncer
Apr 1st, 2007, 04:55 PM
I changed the referer, still keep getting a 302
I can't see anything wrong with the code, it should echo back the contents of the page :s
visualAd
Apr 1st, 2007, 07:27 PM
3xx responses mean you need to look at the location header of the response and fetch the content from the URI in the location header.
Pouncer
Apr 1st, 2007, 07:50 PM
Thanks guys, but I'm still unsure what changes to make to the code.
The script currently gives me this response
HTTP/1.1 302 Found Date: Mon, 02 Apr 2007 00:46:06 GMT Server: Apache/2.0.46 (Red Hat) Location: http://www.onelook.com/index.php3?e=1 Content-Length: 305 Connection: close Content-Type: text/html; charset=iso-8859-1
Found
The document has moved here.
--------------------------------------------------------------------------------
Apache/2.0.46 (Red Hat) Server at www.onelook.com Port 80
penagate
Apr 2nd, 2007, 12:21 AM
First you need to fix your Accept header. Since you aren't prepared to handle image/gif or any of those other types, it is wise not to advertise that you are. Accept: text/html will do fine. It is also nice to make up your own user agent string, to avoid skewing people's statistics.
That said, you need to do as visualAd advised and act upon the 3xx redirection response. Sockets require a firm grasp of the protocol involved. I suggest you read, or at least refer to, the document HTTP/1.1: Status Code Definitions (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). As you can find out there, 302 Found means that you must perform another GET request to the URI indicated by the Location header of the 302 response.
Note that you should keep track of how many redirections you perform, in order not to wind up in an endless loop. Mozilla's default limit is 20 redirections; 10 is plenty.
TheBigB
Apr 2nd, 2007, 12:28 AM
maybe a direct solution would be
$request = "GET /index.php3?w=cat&ls=a HTTP/1.1\r\n";
EDIT:
this is only for as long the redirection exists
Pouncer
Apr 2nd, 2007, 06:16 AM
Thanks so much guys!!!!! TheBig it worked!
<?php
$host = "www.onelook.com";
$fp = fsockopen($host, 80, $errno, $errdesc) or die("Connection to $host failed");
$request = "GET /index.php3?w=cat&ls=a HTTP/1.0\r\n";
$request .= "Accept: */*\r\n";
$request .= "Referer: http://www.onelook.com/\r\n";
$request .= "Accept-Language: en-us\r\n";
$request .= "Host: www.onelook.com\r\n";
$request .= "Connection: Keep-Alive\r\n\r\n";
fputs($fp, $request);
while(!feof($fp)){
$page[] = fgets($fp, 1024);
}
fclose($fp);
for($i=0; $i<count($page); $i++){
echo $page[$i];
}
?>
Now I need help on parsing. The above example looks up the word 'cat'. What I want to do is write the word to a text file called nouns_adjectives.txt if it finds from the incoming data that the word is a noun or an adjective (regardless of whatever else it is)
Can anyone help me with this please?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.