Weekly Tip 2: Using Snoopy to Fetch Content March 11, 2006
Posted by weeklytips in Uncategorized.trackback
Sorry for being a day late on this week’s tip, I got caught up in life last night. This week’s tip is a quick, but extremely useful tip. Often times plugins need to fetch some content over the web, or send a request back to a server to report some info. However, often times for security purposes, webhosts have disabled url wrappers for functions like fopen, leaving you without an easy way to send your request.
WordPress avoids this problem with a handy class called Snoopy, which uses sockets to make a request to remote servers and avoid the problem. Here’s a quick rundown on using Snoopy for some simple tasks.
/* First, we need to make an instance of the class */
$snoopy = new Snoopy();
/* Now we can fetch a url from the web */
$result = $snoopy->fetch(‘http://weeklytips.wordpress.com’);
/* Check to make sure that it was fetched successfully*/
if($result) {
/* Now the variable $snoopy->results contains the information you just fetched, for now we will just echo it out */
echo $snoopy->results;
} else {
echo ‘We were not able to complete your request’;
}
The power of Snoopy goes way beyond simple fetching of URLs, but for now this is all I will cover here. There are tons of good resources and tutorials for Snoopy on the web, so if you need to learn more, you can check those out for now. At some later date, I may also publish the portion of the book on some of Snoopy’s more advanced features, and how they relate with the rest of WordPress.
You miss out the need for
require_once( ABSPATH . ‘wp-includes/class-snoopy.php’);
wishing you well on this site – there are precious few resources out there on “how to do it right”. a lot of stuff out there is only relevant to 1/1.5.
Love to see something on how to make best use of the cache
A
[...] like you I had a “huh?” moment. I didn’t know what the Snoopy library was. This link cleared some things up for me, I hope it [...]
[...] like you I had a “huh?” moment. I didn’t know what the Snoopy library was. This link cleared some things up for me, I hope it [...]
[...] like you I had a “huh?” moment. I didn’t know what the Snoopy library was. This link cleared some things up for me, I hope it [...]