Pear and php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fsensible@gmail.com

    Pear and php

    Does anyone know of any good sites with examples of php and pear - The
    documation on the pear.php site is a bit above me as it doesn't show
    any examples unlike the php.net site.

    I'm mainly looking for examples of the http requests like posting and
    retreiving from sites that require cookies..

  • Chung Leong

    #2
    Re: Pear and php

    Q: How do I retrieve a page from a web site that requires a cookie?
    A: Use stream_context_ create() to create a HTTP context with Cookie as
    one of the headers. Then, if you are coding in PHP 5, pass the context
    to file() or file_get_conten ts() as the third parameter. In PHP 4
    either
    function accepts a context, so you need to open the URL with fopen()
    and
    retrieve the data a chunk at a time with fread().

    Example:

    $opts = array(
    'http'=>array(
    'method'=>"GET" ,
    'header'=>
    "Accept-language: en\r\n" .
    "Cookie: foo=bar\r\n"
    )
    );

    $context = stream_context_ create($opts);
    $f = fopen($url, "rb", false, $context);
    while($data = fread($f, 1024)) {
    echo $data;

    }

    stream_context_ create() is available in PHP 4.3.0 and above. If you are
    using an older version, you would need the cURL functions or use
    fsockopen() to open the connection and send the cookie header with
    fputs().

    Example 1:

    $ch = curl_init();
    curl_setopt($ch , CURLOPT_URL, $url);
    curl_setopt($ch , CURLOPT_HTTPHEA DER,
    array("Cookie: foo=bar"));
    curl_exec($ch);
    curl_close($ch) ;

    Example 2:

    $fp = fsockopen($host , $port);
    fputs($fp, "GET / HTTP/1.0\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Cookie: foo=bar\r\n\r\n ");

    while ($data = fgets($fp, 1024)) {
    echo $data;

    }

    Comment

    • Chung Leong

      #3
      Re: Pear and php

      Just get the page pretending that you're IE. Add "User-Agent:
      Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)" to the HTTP
      headers.

      Comment

      • Andy Hassall

        #4
        Re: Pear and php

        On 3 May 2005 14:56:55 -0700, "fsensible@gmai l.com" <fsensible@gmai l.com>
        wrote:
        [color=blue]
        >Thanks Chung, I've managed to get a response using fsockopen but the
        >data appears to be encoded[/color]

        If you use plain fsockopen you'll end up rewriting an HTTP client, which means
        you'll have all sorts of potential complications to either deal with, or miss
        out (and have it break later). Chung Leong pointed out two other ways of doing
        this; with PHP streams, or with cURL, both of which implement HTTP clients so
        you don't have to deal with anywhere near as much.

        --
        Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
        <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

        Comment

        • fsensible@gmail.com

          #5
          Re: Pear and php

          I was sending the same headers as a browser request and there's quite a
          few.The info is gzip encoded I just assume that IE6 inflates it.

          fputs($fp, "Accept: image/gif, image/x-xbitmap, image/jpeg,
          image/pjpeg, application/vnd.ms-excel, application/msword,
          application/x-shockwave-flash, */*\r\n");
          fputs($fp, "Referer:
          http://www.betfair.com/betting/LoadBetsDataAct ion.do?mi=27022 63&SO=BL&MB=on& AR=on&MBV=AVG&B I=on\r\n");

          fputs($fp, "Accept-Language: en-gb\r\n");
          fputs($fp, "Accept-Encoding: gzip, deflate\r\n");
          fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
          5.1; www.ASPSimply.com; .NET CLR 1.0.3705; .NET CLR 1.1.4322)\r\n") ;
          fputs($fp, "Host: $host\r\n");

          the above plus all the cookie data

          require_once "Request.ph p";
          $req =& new HTTP_Request("h ttp://www.betfair.com/");
          $response = $req->sendRequest( );
          if (PEAR::isError( $response)) {
          echo $response->getMessage() ;
          } else {
          print_r($req->getResponseCoo kies());
          }
          echo $response;

          I've tried curl but with no luck thats why I wanted to give pear a go
          but couldn't find any decent examples to start off.

          Comment

          • fsensible@gmail.com

            #6
            Re: Pear and php

            I was sending the same headers as a browser request and there's quite a
            few.The info is gzip encoded I just assume that IE6 inflates it.

            fputs($fp, "Accept: image/gif, image/x-xbitmap, image/jpeg,
            image/pjpeg, application/vnd.ms-excel, application/msword,
            application/x-shockwave-flash, */*\r\n");
            fputs($fp, "Referer:
            http://www.betfair.com/betting/LoadBetsDataAct ion.do?mi=27022 63&SO=BL&MB=on& AR=on&MBV=AVG&B I=on\r\n");

            fputs($fp, "Accept-Language: en-gb\r\n");
            fputs($fp, "Accept-Encoding: gzip, deflate\r\n");
            fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
            5.1; www.ASPSimply.com; .NET CLR 1.0.3705; .NET CLR 1.1.4322)\r\n") ;
            fputs($fp, "Host: $host\r\n");

            the above plus all the cookie data

            require_once "Request.ph p";
            $req =& new HTTP_Request("h ttp://www.betfair.com/");
            $response = $req->sendRequest( );
            if (PEAR::isError( $response)) {
            echo $response->getMessage() ;
            } else {
            print_r($req->getResponseCoo kies());
            }
            echo $response;

            I've tried curl but with no luck thats why I wanted to give pear a go
            but couldn't find any decent examples to start off.

            Comment

            • fsensible@gmail.com

              #7
              Re: Pear and php

              Managed to finally get the data unencoded by ommitting the line

              fputs($fp, "Accept-Encoding: gzip, deflate\r\n");

              and replacing it fputs($fp, "Accept: text/plain, text/html\r\n"); so
              it assumes my browser doesn't accept gzip data.......now why didn't any
              of you tell me that :)

              Code does seem to take ages to connect though.

              Have to see if there's a problem passing all the cookie data which is
              long.

              Thanks for the help anyone who contributed

              Comment

              • fsensible@gmail.com

                #8
                Re: Pear and php

                OK all sorted, Chungs code didn't close the fsockopen connection soit
                waited for the timeout before displaying the result

                Comment

                Working...