Wednesday, July 22, 2009

When PHP stands for Possibly Hopless Progam

So, here I am, almost broke. Drank my last pot of coffee, down to 6 cigarettes and I get this seemingly simple gig to fix some XML fetch parse present script. Woot I can do that for like $20 USD .. ( I found if I don't specify USD I'm likely to be paid in pesos, rubles or some other currency, exchange rates rarely work to my favor)

Anyways.. It looked like a pretty well written script, using PHP's DOMDocument support, which I've used on some of my own scripts. The crux of the script was...


$rssfeedurl = 'http://someXMLurl';

if (!$xmlDoc = new DOMDocument()){
echo 'Could not initiate feed '.$rssfeedurl ; exit();
}

if (!$xmlDoc->load($rssfeedurl)){
echo 'Could not fetch feed '.$rssfeedurl ; exit();
} else {
$channel = $xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = @$channel->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue;

//etc etc . . . .
}

Of course the script ran for months just fine, then suddenly stopped for no apparent reason. Turns out it was getting a 403 forbidden response.. but the IP was not blocked by the host serving the XML.. actually they couldn't figure it out either.

I tested every conceivable cause I could think of. Shelling into the server, I could wget the XML just fine.. So I wrote a quick PHP script, using curl to fetch the feed, thinking it's gotta be the user agent they are blocking...

I tried all sort of different user agent names, even some real nasty ones that any network would ban on sight. All were allowed to fetch the feed.

Then I accidentally fired the script off with no user agent set. WHOOP THERE IT IS.. 403 forbidden.. BINGO. They reject requests with no user agent set.. but how the hell do you set an agent name for DOMDocument::load ?

Luckily I found the answer. The trick is adding a few lines before you initialize DOMDocument


$opts = array(
'http' => array(
'user_agent' => 'xml fetcher 1.0',
)
);

$context = stream_context_create($opts);
libxml_set_streams_context($context);



if (!$xmlDoc = new DOMDocument()){
echo 'Could not initiate feed '.$rssfeedurl ;
}




So.. now I got $20 USD YAY! A pound of coffee, maybe some top ramen and a pack of smokes!! NEXT !

PS. if you wre actually curious what PHP stand for, it's PHP: Hypertext Preprocessor so. in essence the first P stand for nothing, it is just there to create a cooler sounding 3 letter acronym, and beside HP was taken.

Thursday, July 9, 2009

My 401k is now 404

Well, so much for retiring any time soon. Granted, in another 10 years or so, my 401k will almost certainly regain what it has lost in recent years, but any hopes of an early retirement have been dashed on the jagged rocks below.


I still think having a 401k is a good idea, and strongly suggest anyone with a full time job start one, and divert as much as your income to it as you can. If you start one at age 18, and put as little as 2% into it, you can pretty much assure a comfortable retirement.


There are a couple of strings attached, like early withdrawal penalties, so take my advice, don’t treat a 401k as a savings account. Use a regular bank for that. Only a percentage of “disposable” income should be put into a 401k


For those like me, that started their 401s later in life, you’ll need to invest a larger percentage, as close to the non-taxable max as you can, which is typically %15, but can vary based on your income.


The real beauty of a 401k is the income you put into it is not taxed, and you only pay taxes on your income after the 401 deposit has been subtracted. It’s like you are being paid to save money.


Add to that, the money you’ve invested in your 401k, is exactly that, an investment which can gain or lose money depending on the stock market. Whether you cash out at a profit or a loss is just a matter of timing.

Wednesday, July 8, 2009

When will a penny be worth something?

For a long time now, I’ve been a vocal advocate for the abolishment of the penny, but the US mint continues making them, despite the fact that they cost more to make than they are worth. Perhaps they have considered something I haven’t?


Coins throughout history have been made of precious and semi-precious metals, because there is actual value in gold, silver, nickel and copper. Take the silver dollar, or the $20 gold piece for example. Just like the penny, they were made of metals that actually had value, namely gold and silver. but not any more.



Today’s penny only contains roughly 2% copper, the rest is a zinc based alloy, but even still, the alloy in a present day penny is worth more than the penny itself. Add to that the costs of labor and machinery it takes to make a penny, you wind up with each penny costing around 1.3 cents to make.



Back in the day, gold and silver coins were quickly discontinued when the metals used to make them cost more than the coin was worth, so why not the penny ? What does the government know that I don’t? Are they hoping the value of the dollar will drop so low that manufacturers and retailers will lower their costs? Do they plan to start lowering federal and state mandated minimum wages?