Skip to content

Code :: Blogs, php and URI access

This will be a shorty, as the week has been turbulent and sweet.

So a couple months back, I posted about wp-melative, and in the code I was using a “socket download” since, the opening of URI has been disabled? on AB.net; I didn’t look into it much, just used socket connections as a workaround.

The Problem

Shortly after using the socket download wrapper-func, I noticed odd digits and characters appearing in the RSS. A very bad thing since, these renegade characters would sometimes appear in xml tags, thus invalidating and breaking the chain of events (the sidebar). Again, I didn’t look to much into it, but thought of another solution, this one is easy, and is today’s code.

Code

function wget($uri, $dest)
{
	// renamed from exec
	return EXECUTE( "wget -O $dest '$uri'" );
}

function getXmlFromWget( $uri, $rssfile )
{
	// do the wget
	wget( $uri, $rssfile );
	
	// get destination contents
	$rv = file_get_contents( $rssfile );

	// arbitrary length checking
	return strlen($rv)>5?$rv:false;
}

Techincals

The two functions are very simple. getXmlFromWget() takes the URI and the destination rssfile. It calls the wget function and passes the same parameters. wget() uses php’s exec built-in to pass a terminal command to the system, in this case it is specifically wget.

There should be a word of caution when passing stuff to exec, and especially with wget, since it can download/mirror an entire site with certain switches. I have only included the modest -O switch implying the single file download will be output to $dest.

Is this better than socket download? I believe so, wget seems more stable than php socket looping, and I haven’t noticed any strange characters appearing in the output. There is one small problem of downloading or not downloading, as it wget appears to receive blank files sometimes, hence the reason I include a file length check.

Usage

Since AB.net has limited URI access, the wget wrapper is a good alternative. This would be good for any bloggers wanting load up offsite RSS/XML/API data, as there needs to be a way to obtain the data that isn’t on AB.net. Wget manages this, as well as caching the data to the file system.

What’s that you say? Javascript… sorry XSS is usually a no-no.

RyanA

Note: this was written 6 hours ago, but for some reason WP rejected the above code, until I renamed the ‘exec’ function to EXECUTE… weird

Categories: Meta, Technology, Weekly.

Tags: ,

Comment Feed

No Responses (yet)



Some HTML is OK

or, reply to this post via trackback.