tags:

views:

73

answers:

3
+2  A: 

You can use cURL to grab a webpage, and you may parse it with any regular expression you wish ! http://www.php.net/manual/en/book.curl.php

Rodolphe
That doesn't count! He said "easily"... xP
Alix Axel
@Alix aside from the php man page linked being a french version, there is really nothing that makes using cURL any harder than using fopen, with an added benefit of being able to easily adjust the HTTP request just the way you need it to be.
code_burgar
Sorry for the french version ! fixed this.Actually I have to blame Google, they always push the localized content, which is annoying when looking for some technical docs :)
Rodolphe
@code_burgar: I used curl a lot and I know how powerful it can be, but I haven't yet seen a one liner of php/curl code (init, FAILONERROR, FOLLOWLOCATION, RETURNTRANSFER, SSL_ * 2, exec and close = 8 lines) that can fetch a simple page and return it's contents like `file_get_contents()` it's what the OP wants and needs. Why complicate things?
Alix Axel
@Rodolphe: Are you sure it was Google's fault? The PHP docs always redirects me to the localized version also.
Alix Axel
+1  A: 

You could use file_get_contents

Straight from php.net

<?php
    $homepage = file_get_contents('http://www.example.com/');
    echo $homepage;
?>
nc3b
Note that remote files opening might be disabled by your provider due to security reasons.
christian studer
@christian studer Indeed. That would be a stopper for any _legitimate_ method of getting the contents of an external website.
nc3b
@nc3b why would using cURL not be a legitimate method?
code_burgar
@code_burgar: I didn't say curl is not a legitimate method. But if a provider would go to the trouble of disabling `allow_url_fopen`, he would probably disable all other means of getting remote content
nc3b
@code_burgar: Because it opens remote files? "remote files opening might be disabled by your provider due to security reasons" – christian studer
Alix Axel
@nc3b actually, I've seen many examples where allow_url_fopen was disabled for security reasons while libcurl was allowed and working :)
code_burgar
@code_burgar: Any host who does that it's not worth the money.
Alix Axel
@Alix I beg to differ. "P1: Remote Code Execution - The most widespread PHP security issue since July 2004 is remote code execution, mostly via file system calls. The root causes of this issue are:---snip--- **allow_url_fopen and PHP wrappers allow this behavior by default, which is unnecessary for most applications.
code_burgar
@code_burgar: Any programmer that doesn't check input and suffers from remote code execution is not much of a programmer... That's like having magic_quotes on to avoid SQL injections. That's a task for the developers, not the language. And then again, any host that doesn't recognizes that it's not worth the money.
Alix Axel
@Alix Which is exactly what I would say for any host that doesn't limit or prevent common attack vectors from being used on their machines. Shared servers + vulnerable config settings = not much of a host. As for checking input, you said it yourself, the OP wants a quick fix, why complicate things? ;)
code_burgar
@code_burgar: No one is encouraging the use of eval or include($url). In my experience a lot of hosts simply don't have the cURL extension available, however they often use a hardened PHP patch such as Suhosin which allows file_get_contents (but disallows includes) to open remote files. At least now the OP knows his options. =P
Alix Axel
thanks for the code. It was helpful.
Karthik Kottapalli
@Alix agreed :)
code_burgar
A: 

As Rodolphie already said, using cURL is possibly the best way to fetch a remote page. You really don't need to use regexp to check if the text contains a string, this will do it:

if (strpos($urlContents, $needle) !== FALSE) {
  echo "match found";
}
code_burgar
You're right, I forgot to add this alternative.
Rodolphe
thanks a lot! I used your code to nail it down perfectly.
Karthik Kottapalli