fopen() with url and search engine (beginner)

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

    fopen() with url and search engine (beginner)

    Dear group,



    When I went through the php docs I come across with fopen() function
    which can also takes the any url. So I tested with this following little
    code.



    <?php

    $fh =
    fopen("http://www.google.co.i n/search?hl=en&q= php&btnG=Google +Search&meta=",
    "r");



    while(!feof($fh ))

    {

    $output = htmlspecialchar s(fgets($fh, 1024));

    echo ("$output<br />");

    }



    fclose($fh);



    ?>



    The query string does that all. If I want to have my own page with a text
    box and a submit button which searches the google and display those google
    search result links in my web page. Can that be possible? Is this the basic
    thing about search engine? Or I am I missing something very basic? Thanks
    for any help.


  • Giovanni R.

    #2
    Re: fopen() with url and search engine (beginner)

    On Tue, 3 Jul 2007 13:24:23 +0530, "sathyashra yan"
    <sathyashrayan@ REMOVEggmmaaiil l.comwrote:
    If I want to have my own page with a text
    box and a submit button which searches the google and display those google
    search result links in my web page. Can that be possible?
    You need to set the 'q' var in the query string you pass to google.com.

    In your page.php:

    ....
    <form action="page.ph p" ... >
    <input name="str" type="text" />
    <input type="submit" value="Search" />
    </form>

    <?php
    if (isset($_POST['str'])) {

    $str = $_POST['str'])
    $fh = fopen('http://www.google.co.i n/search?q=' . $str, 'r');
    ...
    fclose($fh);

    }
    ?>

    However, if you have already sent any html (eg. that <form>) you should
    not print out again the <html>, <head>, <body>, etc, html tags coming
    from google.co.in. You should strip them out. Take a look at file() or
    file_get_conten ts() or, better, see <http://www.google.com/apis/>.

    Giovanni

    --
    Advanced PHP Programming? See <http://snipr.com/1nsyy>
    Ajax in Practice? See <http://snipr.com/1nsyz>
    "I bougth them on Amazon.com, I swear I'll sell them only once."

    Comment

    • sathyashrayan

      #3
      Re: fopen() with url and search engine (beginner)


      "Giovanni R." <leonida1818@NO SPAMlibero.itwr ote in message
      news:2v7k83990e o16p7udud2v67go ptnrudhmb@4ax.c om...
      On Tue, 3 Jul 2007 13:24:23 +0530, "sathyashra yan"
      <sathyashrayan@ REMOVEggmmaaiil l.comwrote:
      >
      >If I want to have my own page with a text
      >box and a submit button which searches the google and display those
      >google
      >search result links in my web page. Can that be possible?
      >
      You need to set the 'q' var in the query string you pass to google.com.
      >
      In your page.php:
      >
      ...
      <form action="page.ph p" ... >
      <input name="str" type="text" />
      <input type="submit" value="Search" />
      </form>
      >
      <?php
      if (isset($_POST['str'])) {
      >
      $str = $_POST['str'])
      $fh = fopen('http://www.google.co.i n/search?q=' . $str, 'r');
      ...
      fclose($fh);
      >
      }
      ?>
      >
      However, if you have already sent any html (eg. that <form>) you should
      not print out again the <html>, <head>, <body>, etc, html tags coming
      from google.co.in. You should strip them out. Take a look at file() or
      file_get_conten ts() or, better, see <http://www.google.com/apis/>.
      >
      Thanks a lot.. it a very useful information. So each search has it's own way
      of query string. Is it? yahoo has it own.. altavista has it's own..
      wont they change it in the future..?



      Comment

      • Jerry Stuckle

        #4
        Re: fopen() with url and search engine (beginner)

        sathyashrayan wrote:
        "Giovanni R." <leonida1818@NO SPAMlibero.itwr ote in message
        news:2v7k83990e o16p7udud2v67go ptnrudhmb@4ax.c om...
        >On Tue, 3 Jul 2007 13:24:23 +0530, "sathyashra yan"
        ><sathyashrayan @REMOVEggmmaaii ll.comwrote:
        >>
        >>If I want to have my own page with a text
        >>box and a submit button which searches the google and display those
        >>google
        >>search result links in my web page. Can that be possible?
        >You need to set the 'q' var in the query string you pass to google.com.
        >>
        >In your page.php:
        >>
        >...
        ><form action="page.ph p" ... >
        ><input name="str" type="text" />
        ><input type="submit" value="Search" />
        ></form>
        >>
        ><?php
        >if (isset($_POST['str'])) {
        >>
        >$str = $_POST['str'])
        >$fh = fopen('http://www.google.co.i n/search?q=' . $str, 'r');
        >...
        >fclose($fh);
        >>
        >}
        >?>
        >>
        >However, if you have already sent any html (eg. that <form>) you should
        >not print out again the <html>, <head>, <body>, etc, html tags coming
        >from google.co.in. You should strip them out. Take a look at file() or
        >file_get_conte nts() or, better, see <http://www.google.com/apis/>.
        >>
        >
        Thanks a lot.. it a very useful information. So each search has it's own way
        of query string. Is it? yahoo has it own.. altavista has it's own..
        wont they change it in the future..?
        >
        >
        >
        Why should they? It works fine for them.

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

        Comment

        • sathyashrayan

          #5
          Re: fopen() with url and search engine (beginner)

          Thanks for all the help.. It helped me a lot..


          Comment

          Working...