Snoopy example
Shows how an example how you can use the Snoopy class for doing HTTP requests to other websites.
/* You need the snoopy.class.php from http://snoopy.sourceforge.net/ */ include("snoopy.class.php"); $snoopy = new Snoopy; // need an proxy?: //$snoopy->proxy_host = "my.proxy.host"; //$snoopy->proxy_port = "8080"; // set browser and referer: $snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; $snoopy->referer = "http://www.jonasjohn.de/"; // set some cookies: $snoopy->cookies["SessionID"] = '238472834723489'; $snoopy->cookies["favoriteColor"] = "blue"; // set an raw-header: $snoopy->rawheaders["Pragma"] = "no-cache"; // set some internal variables: $snoopy->maxredirs = 2; $snoopy->offsiteok = false; $snoopy->expandlinks = false; // set username and password (optional) //$snoopy->user = "joe"; //$snoopy->pass = "bloe"; // fetch the text of the website www.google.com: if($snoopy->fetchtext("http://www.google.com")){ // other methods: fetch, fetchform, fetchlinks, submittext and submitlinks // response code: print "response code: ".$snoopy->response_code."<br/>\n"; // print the headers: print "<b>Headers:</b><br/>"; while(list($key,$val) = each($snoopy->headers)){ print $key.": ".$val."<br/>\n"; } print "<br/>\n"; // print the texts of the website: print "<pre>".htmlspecialchars($snoopy->results)."</pre>\n"; } else { print "Snoopy: error while fetching document: ".$snoopy->error."\n"; }
Snippet Details
-
AuthorJonas John
-
LicensePublic Domain
-
LanguagePHP
-
Created05/29/2006
-
Updated05/29/2006
-
Tagsconnections, network, http
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
EvNix July 04, 2010 at 13:20
is there anyway to use POST method
Anon 2 February 14, 2010 at 00:09
@Anon:
No, probably not. But it could be slightly faster than file_get_contents though,
No, probably not. But it could be slightly faster than file_get_contents though,
Anon October 20, 2009 at 15:04
In terms of simply fetching content from a website, is there anything that snoopy better that would make me use it over something like file_get_contents?
lemoussel March 31, 2009 at 22:26
Snoopy is a PHP class that simulates a web browser. It automates the task of retrieving web page content and posting forms.
Mark March 19, 2009 at 11:45
What is Snoopy? What does it do?
MAski July 11, 2008 at 18:08
I have been dealing with snoopy for the past couple of weeks, at last Im glad with what can be done with it, thanks.
nancoder June 20, 2008 at 06:05
thanks! works great!
basti February 09, 2008 at 22:19
This is really great, thanks a lot for this help :-)