Results 1 to 10 of 10

Thread: PHP sockets - expert help perhaps needed.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    PHP sockets - expert help perhaps needed.

    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?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP sockets - expert help perhaps needed.

    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?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: PHP sockets - expert help perhaps needed.

    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 Code:
    <?php
        $host 
    "www.onelook.com";
        
    $fp fsockopen($host80$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($fp1024);
        }
        
        
    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!

  4. #4
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: PHP sockets - expert help perhaps needed.

    Quote Originally Posted by www.W3.org - RFC 2616
    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
    Delete it. They just clutter threads anyway.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: PHP sockets - expert help perhaps needed.

    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

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP sockets - expert help perhaps needed.

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: PHP sockets - expert help perhaps needed.

    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

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP sockets - expert help perhaps needed.

    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. 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.
    Last edited by penagate; Apr 2nd, 2007 at 12:24 AM.

  9. #9
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: PHP sockets - expert help perhaps needed.

    maybe a direct solution would be
    PHP Code:
    $request "GET /index.php3?w=cat&ls=a HTTP/1.1\r\n"
    EDIT:
    this is only for as long the redirection exists
    Delete it. They just clutter threads anyway.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: PHP sockets - expert help perhaps needed.

    Thanks so much guys!!!!! TheBig it worked!

    PHP Code:
    <?php
        $host 
    "www.onelook.com";
        
    $fp fsockopen($host80$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($fp1024);
        }
        
        
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width