Results 1 to 20 of 20

Thread: Look For Specific Text In A PHP Webpage

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Look For Specific Text In A PHP Webpage

    Im using this ti search for specific text in a webpage:

    HTML Code:
    <?php
    $link = "http://www.test.com/";
    $linkcontents = file_get_contents($link);
    	
    $needle = "Hello";
    if (strpos($linkcontents, $needle) == false) {
    echo "String not found";
    } else {
    echo "String found";
    }
    ?>
    But when I try searching a .php webpage I get errors. Any ideas ?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Look For Specific Text In A PHP Webpage

    really? you get errors? what kind of errors?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Look For Specific Text In A PHP Webpage

    Here is the error that its displaying:

    Warning: file_get_contents(http://www.test.com/member.php?) [function.file-get-contents]: failed to open stream: HTTP request failed!

    Thats not the URL, its just an example.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Look For Specific Text In A PHP Webpage

    are you trying to get the file itself, or the rendered output of the file? two very different things...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Look For Specific Text In A PHP Webpage

    Im just trying locate a specific line of text or string on a php page and echo yes if its found and no if its not found.

  6. #6
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Look For Specific Text In A PHP Webpage

    "HTTP request failed" means that the URL you put in file_get_contents() was not found, or had some other problem such that the URL could not be accessed. Make sure your URL is valid and publically accessible - try going to the URL in your browser.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Look For Specific Text In A PHP Webpage

    Yes the page is publically viewable, I wonder is it because the page is php and the rendered content is not actually in the file , its the php thats rendering the information onto the webpage itself ?

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Look For Specific Text In A PHP Webpage

    I think it's more than that... file_get_contents gets the contents of the FILE... which isn't the same as getting the rendered output... for good reason, the poster SHOULDN'T be able to get to the contents of a PHP file... that's why I asked if he was trying to get the file itself or the rendered out put. I'm still not sure I got an acceptable answer.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Look For Specific Text In A PHP Webpage

    Im sorry, Im trying to get a specifis string in the rendered out put of , example (http://www.test.com/member.php?)

  10. #10
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Look For Specific Text In A PHP Webpage

    If you use file_get_contents() with a URL (not a file path), it will get the markup of the rendered output of the page.

    And that aside, a failed HTTP request seems unambiguous to me; your script could not access the resource you requested in file_get_contents(). When I go to "http://www.test.com/member.php?" I get an error 404 - an HTTP error signifying that the resource isn't there. I think you said this isn't the actual URL you're using, but if you want an example, try "http://www.test.com" instead (or "http://www.example.com"), which does not return any HTTP errors when you try to access it.
    Last edited by SambaNeko; Jun 22nd, 2011 at 12:51 PM.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Look For Specific Text In A PHP Webpage

    Yes thats true, but I need to get the text string from "http://www.test.com/member.php" << example url

  12. #12
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Look For Specific Text In A PHP Webpage

    You can't get the text string of the page if the page does not exist. As far as I can see, "http://www.test.com/member.php" does not exist (are you not seeing "Server Error - 404 - File or directory not found" when you try to access this URL in a browser?). If you're using this URL as an example to test, it will not work.

  13. #13
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Look For Specific Text In A PHP Webpage

    I'm pretty sure he's still using that as simply an example to get the idea across to us (and he's not literally trying to load that page in his script).

    dunlop03, we might not be able to help you figure out what's wrong if you don't give us the actual code you're using and getting this error with. file_get_contents() does exactly what you want it to do: it will load a local file or load the rendered output of a file when using a URL.

    However, file_get_contents() (like fopen and other file-opening functions) requires the fopen wrappers to be enabled (as noted in the documentation for the function). You can enable these wrappers by setting the PHP ini directive allow_url_fopen to true.
    Like Archer? Check out some Sterling Archer quotes.

  14. #14
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Look For Specific Text In A PHP Webpage

    However, file_get_contents() (like fopen and other file-opening functions) requires the fopen wrappers to be enabled
    PHP ought to produce a different error if this were the problem.

  15. #15
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Look For Specific Text In A PHP Webpage

    I'm not sure whether or not it would. But, considering how little information has been given in this thread as to the actual URL being used, it's the only other thing I could think of if dunlop03 is really doing everything properly.
    Like Archer? Check out some Sterling Archer quotes.

  16. #16
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Look For Specific Text In A PHP Webpage

    I'm not sure whether or not it would.
    It would, I checked; if allow_url_fopen is Off, you get "http:// wrapper is disabled in the server configuration by allow_url_fopen", followed by "failed to open stream: no suitable wrapper could be found in ...". As opposed to when allow_url_fopen is On, and I try fetching a non-existent page, I get the exact error that dunlop03 posted: Failed HTTP Request.

    I know that's not absolute proof, but it seems solid enough to me. Of course it doesn't hurt to just check the status of allow_url_fopen anyway.

    It's funny because the exact code that dunlop03 posted in the original post works just fine for me. It's only the insistence upon this 404'ing URL where it doesn't work. I get that "it's just an example," but examples are for illustrative purposes: your example does not work for reason X and it's possible that your real URL is not working for reason X also. If you're not going to give us the real URL, then we only have the example to work with.

    TL;DR: kows already said the important bit:
    dunlop03, we might not be able to help you figure out what's wrong if you don't give us the actual code you're using and getting this error with.
    Last edited by SambaNeko; Jun 23rd, 2011 at 01:32 PM.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Look For Specific Text In A PHP Webpage

    Sry for the late reply.

    Ok for example, If i click on one of your usernames here and view your profile page , Im trying to find specific text/string or html element, like so
    Code:
    <img src="image here"/>
    I'll use SambaNeko's profile as an example http://www.vbforums.com/member.php?u=115476 . If you view his page, under his username it displays "Frenzied Member" , I want this script to detect that text and echo "String Found"

    Or

    I want it to detect a html element, for example on SambaNeko's profile again, theres an image
    Code:
    <img title="SambaNeko is offline" class="inlineimg" src="images/statusicon/user_offline.gif" alt="SambaNeko is offline" border="0">
    I want the script to detect that specific html element and echo "String Found"

    I hope you understand what Im trying to acheive, thanks again guys.

  18. #18
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Look For Specific Text In A PHP Webpage

    The problem is that the profile pages are only available when you're logged in.
    A plain file_get_contents() does not have a session like you have when you visit the page in the browser.
    Delete it. They just clutter threads anyway.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Look For Specific Text In A PHP Webpage

    That may be the case here, but the actuall forum im using allows guests to view the profile page. So the page is publically viewable.

  20. #20
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Look For Specific Text In A PHP Webpage

    That may be the case here, but the actuall forum im using allows guests to view the profile page. So the page is publically viewable.
    Then you can use the same code you posted in your very first post: there is nothing wrong with your code. The only problems you have made clear to us are problems with HTTP access - either because the URL was not found, or because the URL requires login.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width