Accessing a URL from a PHP Script

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

    Accessing a URL from a PHP Script

    Is there any way to access a URL from within my PHP CLI script?
    Basically, I want to use my script to "hit" a web page on a remote
    server. I then intend for that remote server to perform an action
    based on the configuration for that page.

    How do I initiate a call to the remote server's URL from within PHP?

    Thanks,
    justbn

  • justbn

    #2
    Re: Accessing a URL from a PHP Script

    I have made some progress but still have a problem. I can do this
    using the following :

    <?php
    header("Locatio n: http://www.example.com/"); /* Redirect browser */

    /* Make sure that code below does not get executed when we redirect. */
    exit;
    ?>

    Unfortunately, this does not seem to work when the php script is
    executed from the command line. When I call this script via a browser,
    it successfully sends the header.

    Any idea how to do this from the command line?

    Thanks,
    justbn

    Comment

    • hackajar@gmail.com

      #3
      Re: Accessing a URL from a PHP Script

      try Curl or fopen

      $fp = fopen("http://www.example.org/path/to/file.html", "r");

      curl is tricky, and you need to compile it in. If your just trying to
      "hit" a web page, fopen should be fine.



      Comment

      • NC

        #4
        Re: Accessing a URL from a PHP Script

        justbn wrote:[color=blue]
        >
        > Is there any way to access a URL from within my PHP CLI script?
        > Basically, I want to use my script to "hit" a web page on a remote
        > server. I then intend for that remote server to perform an action
        > based on the configuration for that page.
        >
        > How do I initiate a call to the remote server's URL from within PHP?[/color]

        $contents = file_get_conten ts('http://www.example.com/');

        Cheers,
        NC

        Comment

        • justbn

          #5
          Re: Accessing a URL from a PHP Script

          NC and Hacka,

          Thanks for the ideas. Both of these worked perfectly.

          justin

          Comment

          • J Wynia

            #6
            Re: Accessing a URL from a PHP Script

            justbn wrote:[color=blue]
            > NC and Hacka,
            >
            > Thanks for the ideas. Both of these worked perfectly.
            >
            > justin
            >[/color]
            If you want to easily get not only the HTML, but all of the linked CSS,
            JS, images, etc. you can have PHP point to wget, which as pretty robust
            page and site mirroring capabilities. Below is a script and Windows .bat
            file I use to bookmark/save sites remotely. There's a link on my browser
            bookmark bar that sends the current URL to this script, which saves a
            copy of the URL and logs the entry. It's helped me be able to have
            cached copies of good information that goes away 6 months later and
            given me file-based searching of all of the content in my bookmarks.
            fetch.bat should all be one line.

            -------------------------------------
            J Wynia
            Myriad Intellect, Inc.
            "Web technology that earns its keep."

            -------------------------------------

            --------addbookmark.php---------------
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            <html>
            <head>
            <title> New Document </title>
            <script language="JavaS cript">
            <!--
            history.back();
            //-->
            </script>
            </head>

            <body>

            <?php
            $url = $_GET['url'];

            system("pathto/fetch.bat \"$url\"");

            $filename = 'bookmarks.list ';

            if($url){
            $url = urldecode($url) ;

            $content = ("$url\n");
            if (is_writable($f ilename)) {
            if (!$handle = fopen($filename , 'a')) {
            die("Cannot open file ($filename)");
            exit;
            }

            if (fwrite($handle , $content) === FALSE) {
            die("Cannot write file ($filename)");
            exit;
            }

            fclose($handle) ;

            } else {
            die("Cannot write file ($filename)");
            }


            print("$url added");
            } else {
            print("No URL");
            }

            print("<hr><pre >");

            ?>


            </body>
            </html>
            --------addbookmark.php---------------



            --------fetch.bat---------------
            pathto/wget.exe -p --convert-links -P pathwheretosave/sites/ %1
            --------fetch.bat---------------

            Comment

            Working...