jump to navigation

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(‘https://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.

Advertisements

Comments»

1. Alan T - August 3, 2006

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

2. WordPress 2.6.3 : Addressing a low risk vulnerability - October 26, 2008

[…] 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 […]

3. WordPress 2.6.3 : Addressing a low risk vulnerability | ZachBrowne.com - October 31, 2008

[…] 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 […]

4. WordPress 2.6.3 : Addressing a low risk vulnerability | WP Passion - November 9, 2008

[…] 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 […]

5. Extra Tip: Object Oriented Plugin Development - January 8, 2010

[…] Weekly Tip 2: Using Snoopy to Fetch Content […]

6. Weekly Tip 2: Using Snoopy to Fetch Content - January 8, 2010

[…] 5. Extra Tip: Object Oriented Plugin Development – January 8, 2010 […]

7. Blog Face2bouk » Blog Archive » Extra Tip: Removing Admin Menus - January 15, 2010

[…] Weekly Tip 2: Using Snoopy to Fetch Content […]

8. lottcote22414 - April 8, 2016

Sammy loves his “big brother” role and is so nurturing! Click http://link.mx/hool08200


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: