fread usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vito

    fread usage

    a week ago 2 kind persons suggested me to use fread to read a result page.
    i've been working for it but just can't get what i want.


    $keyword ="big problem";
    $startfig = rand(0,60);
    //$theFile
    ="http://images.google.c om/images?q=\"".$k eyword."\"&star t=".$startfig ;
    //$theFile
    ="http://images.google.c om/images?q=\"".$k eyword."\"&star t=".strval($sta rtfig);
    $theFile ="http://images.google.c om/images?q=acd";

    if (($f = fopen($theFile, 'r')) === false) exit;

    $contents = fread($f, (9*(1024*1024)) );

    echo "$contents" ;
    fclose ($f);

    $matchno = preg_match_all( "/.*<img src=/images?q=tbn:.* :(.*)
    width=.*>.*/", $contents, $regs);
    $imgurl = $regs[rand(0,$matchno-1)];

    why can't i retrieve the result obtained from google image search?


  • Henrik Hansen

    #2
    Re: fread usage

    "vito" <vitogen2003@ya hoo.com.hk> writes:
    [color=blue]
    > a week ago 2 kind persons suggested me to use fread to read a result page.
    > i've been working for it but just can't get what i want.
    >
    >
    >
    > why can't i retrieve the result obtained from google image search?[/color]

    Dont you get any result at all in your $content var? no content at
    all?

    Also I would recommend you use file_get_conten ts() instead, it's alot
    easier that fread and fopen.

    --
    Henrik Hansen

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: fread usage

      vito wrote:[color=blue]
      > a week ago 2 kind persons suggested me to use fread to read a result page.
      > i've been working for it but just can't get what i want.
      >
      >
      > $keyword ="big problem";
      > $startfig = rand(0,60);
      > //$theFile
      > ="http://images.google.c om/images?q=\"".$k eyword."\"&star t=".$startfig ;
      > //$theFile
      > ="http://images.google.c om/images?q=\"".$k eyword."\"&star t=".strval($sta rtfig);
      > $theFile ="http://images.google.c om/images?q=acd";
      >
      > if (($f = fopen($theFile, 'r')) === false) exit;
      >
      > $contents = fread($f, (9*(1024*1024)) );
      >
      > echo "$contents" ;
      > fclose ($f);
      >
      > $matchno = preg_match_all( "/.*<img src=/images?q=tbn:.* :(.*)
      > width=.*>.*/", $contents, $regs);
      > $imgurl = $regs[rand(0,$matchno-1)];
      >
      > why can't i retrieve the result obtained from google image search?
      >
      >[/color]
      The url you feed to google does NOT bring me any results at all, instead
      I get this error:
      "The requested URL /?q=%22big%20pro blem%22&start=3 0 was not found on
      this server."

      Looks like google likes another syntax. "big problem" fed to the image
      search page on my google yields the following url:



      Of course your result will be slightly different, you don't get the
      Dutch google I assume. The quotes are never used AFAIK in google.

      Maybe your problem lies here, not in the fread you use.
      Sh.

      --
      Our POP server was kidnapped by a weasel.

      Comment

      • Jerry Stuckle

        #4
        Re: fread usage

        vito wrote:[color=blue]
        > a week ago 2 kind persons suggested me to use fread to read a result page.
        > i've been working for it but just can't get what i want.
        >
        >
        > $keyword ="big problem";
        > $startfig = rand(0,60);
        > //$theFile
        > ="http://images.google.c om/images?q=\"".$k eyword."\"&star t=".$startfig ;
        > //$theFile
        > ="http://images.google.c om/images?q=\"".$k eyword."\"&star t=".strval($sta rtfig);
        > $theFile ="http://images.google.c om/images?q=acd";
        >
        > if (($f = fopen($theFile, 'r')) === false) exit;
        >
        > $contents = fread($f, (9*(1024*1024)) );
        >
        > echo "$contents" ;
        > fclose ($f);
        >
        > $matchno = preg_match_all( "/.*<img src=/images?q=tbn:.* :(.*)
        > width=.*>.*/", $contents, $regs);
        > $imgurl = $regs[rand(0,$matchno-1)];
        >
        > why can't i retrieve the result obtained from google image search?
        >
        >[/color]

        OK, Vito, what happens when you try this?

        One thing I see as a possible problem - you're reading up to 9MB. Do you have
        that much memory available to PHP? You might have run out. Check your error logs.

        BTW - do you have Google's permission? Last I heard they didn't like people
        doing this.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • vito

          #5
          Re: fread usage

          after reading your comments,

          i made some changes.

          1)switch to use "acd" instead of "big problem"
          2)try use file_get_conten ts instead


          $theFile ="http://images.google.c om/images?q=acd";


          the $content contains:

          <img
          src=/images?q=tbn:8w 7TF3ADocTy3M:ch eminf.cmbi.ru.n l/cheminf/isis/pict/acd_price.gif
          width=134 height=105>

          but i just don't know why

          $matchno = preg_match_all( "/.*<img src=/images?q=tbn:.* :(.*) width=.*>.*/",
          $content, $regs);

          returns nothing. ($matchno echo nothing)

          furthermore, how is Jerry able to count the size? could anybody tell me?
          indeed, i need not retrieve the whole content but just a single little image
          is grateful.



          Comment

          • Jerry Stuckle

            #6
            Re: fread usage

            vito wrote:[color=blue]
            > after reading your comments,
            >
            > i made some changes.
            >
            > 1)switch to use "acd" instead of "big problem"
            > 2)try use file_get_conten ts instead
            >
            >
            > $theFile ="http://images.google.c om/images?q=acd";
            >
            >
            > the $content contains:
            >
            > <img
            > src=/images?q=tbn:8w 7TF3ADocTy3M:ch eminf.cmbi.ru.n l/cheminf/isis/pict/acd_price.gif
            > width=134 height=105>
            >
            > but i just don't know why
            >
            > $matchno = preg_match_all( "/.*<img src=/images?q=tbn:.* :(.*) width=.*>.*/",
            > $content, $regs);
            >
            > returns nothing. ($matchno echo nothing)
            >
            > furthermore, how is Jerry able to count the size? could anybody tell me?
            > indeed, i need not retrieve the whole content but just a single little image
            > is grateful.
            >
            >
            >[/color]

            Vito,

            I was looking at your fread() call - you say get up to 9*1024*1024 bytes - which
            is 9 mb.

            And you are getting the correct information back. When you fetch a page
            containing images, the webserver returns <img> tags. The browser must then go
            out and fetch each of those images.

            Open the page with your query and then look at the HTML source. This is what
            your program should be getting. Writing that to the user's browser should cause
            the browser to initiate further connections to retrieve the data.

            As for your regex - ? is a control character (makes the preceding character
            optional). You need to escape it, i.e.

            ... images\?q=tbn ...

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            Working...