Hey there,

I am trying to write a code that accepts user input on one page through a POST method form, then concatenates that data into a url as get variables. I then need to open the page using fopen with the get variables in tact.

Here is what I have, abridged and edited for simplicity. The input form, in a separate file, is functional and has been omitted:

1. $what = $_POST['what'];
2. $where = $_POST['where'];
3. $url = "http://www.example.com/search/listings?what=" . $what . "&where=" . 4. $where;
5. 
6. $openedURL = fopen($url, "r");
7. while(!feof($url))
8.    {
9.      //do something
10.  }

When I try to run this script, I get an infinitely looping error message:
Warning: feof() expects parameter 1 to be resource, string given in script.php on line 7

I suppose what I really need to know is: How do I pass get variables into an fopen? Is that even the problem we're dealing with here?

    Absolute paths in fopen require the ftp connection. IE: [url]ftp://username:password@whatever.com//path/to/file[/url]

    Notice the // after the .com.

    Otherwise you would be able to open any file from any website which would be a massive security risk.

    If you don't want to risk that i would suggest just using a relative path

      Thanks for your timely reply, Zypher.

      Unfortunately, in this situation I must use an absolute path because the url I am trying to open is on a different server. The tutorial where I learned this technique (located here: http://www.wadecybertech.com/2010/05/17/how-to-parse-websites-with-php/) indicates the it is perfectly ok to use an absolute path. Unless the tutorial itself is no good, this is what I am basing this method on.

      The purpose of this script is to scrape public information off of the site to be displayed on my own server.

      If it is not possible to use fopen in conjunction with GET variables or an absolute path, is there an alternative means to this end that anyone can think of?

        To answer my own question (having learned a bit more and extending my search), I found that using $raw = file_get_contents($url), the source code gets stored in $raw and can be manipulated in that fashion. $url can contain GET variable information.

          Yes you can get the contents of a page like that. You can't however use fopen which attempts to open an actual file as opposed to the returned source. Glad you figured it out

            Note that the problem in the original post above was that [man]feof/man expects a file resource handle, whereas you gave it a string instead.

            Either way, don't forget to mark this thread resolved (if it is) using the link on the Thread Tools menu above.

              Marked the post as resolved. Thanks for your help, everyone.

                Just as an aside, [man]http_build_query[/man] might be a more convenient/robust way to construct a query string.

                  Thanks for the additional information, Weedpacket.

                  That certainly looks like a more efficient and proper function to use in this situation.

                    whats the purpose of reading a variable since you defined it yourself.

                    the functions you are speaking of are for reading files. in your code you are actually reading from a file that does not exist. $url - is a Unified Resource Locator which is not a resourse that as brad has said in his post you're specifying in your feof (file end of file) function, which is a boolean and does Returns TRUE if the file pointer is at EOF since you put ! (exclamation mark) before means not at end or FALSE you are running a while loop until it is.

                    My point is that the functions you are using in your code are irrelavent to your solution.

                    sincerely,

                    iia.

                      Hi igorek,

                      If you reread my first post, you'll find that I mentioned that there is a separate file that contains a form that I did not include because it functions fine and is not necessary for someone to help answer my question.

                      The $_POST variables are set by that form, and are then set into the simpler format of $what and $where, which are concatenated into the $url string to form a complete url.

                      Of course, you're right that fopen was not the correct function to use, as I later discovered with further research in PHP scraping processes.

                      I appreciate your help, but understand that I am aware of the things you brought up and politely ask that you please read my post carefully. Most of what you mentioned is pretty basic and I wouldn't want anyone to think that I am asking redundant or simplistic questions.

                        In other words, the test should have been

                        if(!feof($openedURL)) { ... }

                        And yes, that does work on HTTP streams.

                        (That said, I too would use [man]file_get_contents[/man] unless I'm expecting a huge result.)

                          as I understand this correctly you are getting a script from another website based upon your $_POST data replaced in His/Others URL as would happen in his script as get method.

                          My point now if it is your server thus this is fine, altough unsecure. Someone elses all he has to do is change permissions and you wont be able to read or thus file_get_contents().

                          Note: after seeing this I am never hosting my apps on a windows server, unless its local.

                          p.s. I miss typed, URL Uniform, locator, identifier, name

                            Write a Reply...