RSS
 

Archive for the ‘How-To’ Category

Howto: Enable PCNTL in Ubuntu PHP installations

30 Jul

PCNTL in PHP allows for some handy advanced “trickery” using the OS process functions inherent in Linux (*nix?).  I believe some features are available in Windows, but I know for certain that pcntl_fork() is not.

Anyway, it is not enabled by default, so if you want to take advantage of the functions on your Ubuntu LAMP server, you might spend hours searching the web for that magic aptitude command.  But, as far as I can tell, it doesn’t exist.

Luckily, I stumbled across this article on the Ubuntu forums, so I’m dedicating a post here with the hopes that other will find it more easily.

Please note that you’ll probably need build-essentials and a few other source compilation basics, but as long as you have that, the following code will get you what you want.

First, in your home directory:

mkdir php
cd php
apt-get source php5
cd php5-(WHATEVER_RELEASE)/ext/pcntl
phpize
./configure
make

Then:

cp modules/pcntl.so /usr/lib/php5/WHEVER_YOUR_SO_FILES_ARE/
echo "extension=pcntl.so" > /etc/php5/conf.d/pcntl.ini

FYI: “make install” does not appear to put the files in the correct place.

Btw, please direct any thanks/praise to skout23 on the Ubuntu forums.

 
 

FedEx Shipping XML API Idiosyncrasies — the saga continues (PHP & Soap envelopes)

24 May

I am doing a fairly large implementation using the exposed APIs of all 4 major shippers.  I’ve posted a few notes on USPS, and I’ve been using UPS for so long it almost seems to make perfect sense.

So, while I have to applaud FedEx for the granularity and versatility of the API they have exposed, it has caused me severe headaches due to the PHP SimpleXML issues it brings up with SOAP envelopes — particularly nested soap envelopes.

Long story short, the jist of this post is to help others deal with the responses they receive from the FedEx servers (if, like me, their pre-packaged methods do not implement tidily into your application structure).

Here is a sample response of a rate request (NOTE: this is a “Warning” response, only here for demo purposes):


   <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
    <env:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <v8:RateReply xmlns:v8="http://fedex.com/ws/rate/v8">
            <v8:HighestSeverity>WARNING</v8:HighestSeverity>
            <v8:Notifications>
                <v8:Severity>WARNING</v8:Severity>
                <v8:Source>crs</v8:Source>
                <v8:Code>556</v8:Code>
                <v8:Message>There are no valid services available. </v8:Message>
                <v8:LocalizedMessage>There are no valid services available. </v8:LocalizedMessage>
            </v8:Notifications>
            <v8:TransactionDetail xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
                <v8:CustomerTransactionId>TC05_Rate_Package_Groups_POS</v8:CustomerTransactionId>
            </v8:TransactionDetail>
            <v8:Version xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
                <v8:ServiceId>crs</v8:ServiceId>
                <v8:Major>8</v8:Major>
                <v8:Intermediate>0</v8:Intermediate>
                <v8:Minor>0</v8:Minor>
            </v8:Version>
        </v8:RateReply>
    </env:Body>
</soapenv:Envelope>
    

There are lots of ways you might envision accessing that precious “RateReply” tag, but to keep it simple and avoid inferences, you get to it like this:

$xml = simplexml_load_string ($xml_from_above);
$xml->children(‘http://schemas.xmlsoap.org/soap/envelope/’)->Body->children(‘http://fedex.com/ws/rate/v8′)->RateReply;

I do suppose this is obvious enough, but believe me, the nuances that can throw it off can still cause you to inadvertently lose ridiculous amounts of time on it.

Furthermore, if you prefer to do direct PHP cURL XML calls (which you probably do given that you’re reading this), you’ll find that FedEx is not terribly interested in your type of programmer. To find a simple sample XML call, you’ll have a tough time (although I managed to find it in the WSDL downloadable ZIP). However, this is a very basic call. To add additional data (such as the exotic field that apparently no one would ever think to use — “InsuredValue”) — this field has the form (EXACTLY, stuff in brackets is what can be changed) “<InsuredValue><Currency>[currencytype]</Currency><Amount>[float]</Amount></InsuredValue>”

The “InsuredValue” node/field must go immediately before the “Weight” node in the “RequestedPackageLineItems” node. (This of course reference FedEx API v8 — things may be different in future releases.)

 

USPS Shipping XML API Testing Idiosyncrasies

20 May

If you’re a web developer and happen to develop software for use in e-commerce, chances are, somewhere along the lines you’ll need or want to integrate with the big 4 shippers’ (UPS, USPS, FedEx, DHL) APIs.  You’ll find right off the bat that they all offer rather robust APIs, so your options are sufficient.

Then you’ll get to programming and realize that the documentation is pretty crappy, but specifically I want to address the idiosyncrasies of the USPS “test” environment.  Effectively, what USPS means when they say “test” is not a test of robustness of your application, but simply whether or not your application can build a sample request (an EXACT sample request), and send it to their server.  Yeah — it’s like asking a math teacher to write the numbers 1 to 30 on a sheet of paper (in order) before he/she can get hired.

The problem is, the USPS docs don’t tell you this, nor do they show you the sample request.  So, for others who are about to embark on a few hour journey finding these details on Google (or worse, emailing USPS directly …eeek) I’m going to sum up a few facts here.

The most laborious for me so far is the one I already mentioned above.  For a rate request, the docs show you a RateV3Request, but in testing you can only use a RateV2 request (which does not support package dimensions).  Also, you must use the zip codes 10022 and 20008 for origination and zip, as well as 10 lbs. 5 oz. for the weight, and “LARGE” for the size.  Everything else (LAUGH) you have leeway with.

If you don’t use these exact values, you’ll get responses like “Please enter a valid zip code for the sender” (which of course makes you think you wrote the XML incorrectly) or “The package size must be ‘Regular’, ‘Large’, or ‘Oversize.’” (even though you have “regular” quite clearly in the request.

The advice is to get to production as soon as possible, though why USPS would design things this way is beyond me, but them’s the cards, you gotta play ‘em.

I will add more here as I find them obstaclicious enough (yeah I just made up that word).

Amendment 1:  I should add that the issues about the documentation not mentioning the “canned” requests is only applicable to the PDF documentation.  It is stated quite clearly in the HTML versions.  Go figure …

 

Google AdSense rejection and cross-domain duplicate content experience

08 Mar

Since the rejection replies you get from Google regarding applications to their AdSense program can be quite vague and even cryptic, I thought I would share my experience. First things first, you are not going to get to talk to Google directly — you have to use the forums. (Here is my thread.)

My rejection was simply:

Hello Jeremy,

Thank you for your interest in Google AdSense. Unfortunately, after
reviewing your application, we’re unable to accept you into Google AdSense
at this time.

We did not approve your application for the reasons listed below.

Issues:

- Unacceptable site content

Looking around their restrictions, I really could not find a solution, as I couldn’t find any rule that I explicitly violated.  I posted a request for insight on the forums, and a very insightful user (wasaweb) pointed out my extensive duplicate content on http://jeremytharp.com/ and http://www.coffeecuphalfmoons.com/.  And it’s true — I use my blog’s rss to feed much of the content on my music site.

Since I want to keep the sites separate, I saw no reasonable solution, but then I discovered that recently Google began allowing cross-domain canonical URLs.  Eureka!  I simply set the canonical URLs on the pages on jeremytharp.com that fed from coffeecuphalfmoons.com to the appropriate page that fed the same content on coffeecuphalfmoons.com.  When I resubmitted the AdSense application, it was approved!

Keep in mind that there are a LOT of reasons you might get rejected from AdSense, but given that this was very simply successful, I thought I would share.

I also noticed an immediate reorganization of my site:jeremytharp.com results on Google (for the better).  I’m hoping it has some positive effects on PR on both pages as well.

 
 

WORDPRESS HOW-TO: Ignore/Hide A Category On Your Home Page

28 Feb

Sometimes you don’t want a particular category to appear on your WordPress home page.  For me, I create weekly Twitter digests, and I’d rather they not be part of the page welcome.

The process of hiding them actually took me a little bit to figure out, so I thought I’d share.  It’s rather simple, so don’t blink!  :)

Simply edit “index.php” in the wp-content/themes/<your_theme_here>/ directory.

Between the lines:

<?php if (have_posts()) : ?>

and

<?php while (have_posts()) : the post(); ?>

Insert the line:

<?php if (is_home()) { query_posts("cat=-XXX"); } ?>

Be sure to replace “XXX” with the unique numeric ID of the category you want to ignore.  To find the ID, log into your admin, navigate to your categories, and select the one you want to ignore.  The number will then appear in the URL.

NOTE: Make sure you don’t delete the “-” before the “XXX” or else you will ONLY show entries from the category.

If you update your theme, you will probably have to repeat this procedure.

If you want to hide multiple categories simply append the other categories to the string with a comma: (Example: “cat=-12,-82,-4″)

It also follows, then, that this technique can also be used to hide tags, posts, etc.

 

Verizon Wireless USB760 USB Modem Ubuntu Howto

13 Feb

This is a repost of information located at: Verizon Wireless USB760 on the Ubuntu Forums.

I am putting it here because I found it very useful, and wanted people to be able to find it.  If you anyone takes issue with my publishing it here, please contact me.

At any rate, the Verizon USB760 does not work right away, so you have to modify two files.  This solution was posted by Databit. I added a few notes for clarification purposes and enclosed them in brackets.

File 1: /etc/udev/rules.d/70-persistent-cd.rules

Find the line that contains "Novatel_Mass_Storage" and append the following to it:

RUN+="/usr/bin/eject %k"

[You will probably need to use a comma before this part]

File 2: /usr/share/hal/fdi/information/10freedesktop/10-modem.fdi [Will require superuser privileges]

Add this in the USB section:

      <!-- Verizon USB760-->
      <match key="@info.parent:usb.vendor_id" int="0x1410">
        <match key="@info.parent:usb.product_id" int="0x6000">
          <match key="@info.parent:usb.interface.number" int="0">
            <append key="info.capabilities" type="strlist">modem</append>
            <append key="modem.command_sets" type="strlist">IS-707-A</append>
          </match>
        </match>
      </match>

I have tested this and it worked without issue in Jaunty Jackalope 9.04.

 
 

Linux BASH Script: Convert mp3 to avi with static image (command line)

21 Jan

This is a script I made to take advantage of the ffmpeg package in linux to quickly convert an mp3 to avi using a static image. I personally use this technique for uploading my songs to YouTube. I originally found the conversion command here.

All you need is a linux distro with ffmpeg installed, a jpeg or png image, and an mp3. Note: It is highly likely other image formats, audio formats, and output video formats will work, but I have only used jpeg/png+mp3+avi and so cannot attest to results otherwise.

Usage: bash mp32avi.sh <image_file> <mp3_file> <output_file.avi>

Code (mp32avi.sh):

#!/bin/bash
FFMPEG=`which ffmpeg`
if [ "$FFMPEG" = "" ] ; then
	echo "Please install ffmpeg.";
	exit 0;
fi
if [ $# != 3 ] ; then
	echo "Usage: $0 <image_file> <mp3_file> <output_file.avi>";
	exit 0;
fi
if [ ! -f $1 ] ; then
	echo "Source image '$1' not found.";
	exit 0;
fi
if [ ! -f $2 ] ; then
	echo "Source mp3 '$2' not found.";
	exit 0;
fi
if [ -f $3 ] ; then
	echo "Output file '$3' exists.  Overwrite? (y/n)";
	read CONFIRM
	if [ "$CONFIRM" == "y" ] ; then
		echo "Overwriting '$3'"
	else
		if [ "$CONFIRM" == "Y" ] ; then
			echo "Overwriting '$3'"
		else
			echo "Operation canceled.";
			exit 0;
		fi
	fi
fi
TIME=`$FFMPEG -i $2 |& grep 'Duration' | awk '{ print $2; }' | sed -e 's/,//g'`
$FFMPEG -loop_input -i $1 -i $2 -acodec copy -y -t $TIME $3
 

I always wondered how a sewing machine works!

29 Jul

http://home.howstuffworks.com/sewing-machine1.htm

Beautiful diagrams explaining it all.

Actually, I hadn’t really heard of the bobbin-less “chain stitch.” I can see why it was invented first.

However, I could never understand how the top thread with a bobbin worked — now I see the upper thread wraps all the way around the bobbin assembly, looping the top around.

Obviously, there is a lot of thread twisting going on, and I still don’t really see how the top thread goes around the bobbin when it is mounted in on a pin, but at least I get the concept!

 
 

Why GoDaddy can’t get it right, and why you should not use their Grid Hosting

23 Jun

Grid hosting is a somewhat recent technology that essentially emulates a single server, but whose processing power is distributed over multiple servers.  There are several technologies used to accomplish this, but essentially it can be thought of as a cluster or as a self-scaling server arrangement.

The concept of it is to get the best of both worlds — you upload your website to a single server, or at least a single IP address, but depending on how much traffic your site is getting, more servers are added or removed to maintain stability.  In other words, easy, 100%, infinite scalability.

It is very attractive.  However, after spending the last few months using GoDaddy’s Grid Hosting BETA, I simply have to conclude that GoDaddy has it all wrong.  I believe from their perspective, they consider it one of their lower end solutions, and indeed, during the beta, it is only $4.99 a month.  However, infinite scalability is not something of value to someone whose websites will never scale.  Scalability is a point of interest for large sites, especially large sites that could become gargantuan.

So, the truth is, the service works very well with the exception of one important factor:  You can’t directly connect to any hosted file.  That’s right — if you attempt to post data to a script hosted on their server, you will, at least once ever so often, be forced through a 302 File Temporarily Moved redirect.  And, according to the W3C:

The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

      Note: RFC 1945 and RFC 2068 specify that the client is not allowed
      to change the method on the redirected request.  However, most
      existing user agent implementations treat 302 as if it were a 303
      response, performing a GET on the Location field-value regardless
      of the original request method. The status codes 303 and 307 have
      been added for servers that wish to make unambiguously clear which
      kind of reaction is expected of the client.

Summary: Any posted data cannot be forwarded through the redirect.  Again, I’ll reiterate that this only happens on occasion, but it happens 100% of the time for a service I was trying to implement:  PayPal IPN.

That’s right, every time PayPal tries to send an IPN (Instant Payment Notification) they are sent a 302 File Temporarily Moved header.  Given that PayPal sends payment data via POST, and that PayPal is so fixated with security, and that the W3C expressly prohibits the forwarding of posted data through a 302 redirect, well it just doesn’t work.

And ultimately, anyone who uses any form on their website cannot reliably expect to get any results, as this redirect will prohibit the form from working.

The fact of the matter is, it really makes no sense anyway.  A logical conclusion would be, well why not have PayPal submit the data to the destination of the temporary redirect?  Short answer, the redirect goes to the same file.

Let’s say my IPN handler is at http://mysite.com/handler.php.  PayPal sends POST data to that URL, only to receive a 302 reply to redirect to the location http://mysite.com/handler.php?3ecvxYara (3ecvxYara is just a random string of characters, and it can change).  Ok so PayPal is then redirected to http://mysite.com/handler.php?3ecvxYara, where it can’t resubmit the POST data, but that’s where GoDaddy sends them.  Once they hit that URL, they receive yet ANOTHER 302 which directs them back to the original URL, only this time it works.  However, 2 steps ago, we were prohibited from resubmitting the POST data.

Let me summarize, GoDaddy, this SUCKS.

I have spent almost 6 months back and forth with their technical support.  I would send an email, only to get the reply “Hey we got your question.”  Then, a few hours later, I would get a reply “Your request has been forwarded to the high level tech guys because we low-level guys barely know how to turn a computer on.”  Finally, a day or so later, I would get “We think it is a problem with your scripting.  Check your script and make sure you’re not screwing up.”

At this point, all I knew was that PayPal couldn’t connect because they were getting a 302 reply.  I had theorized that there was some kind error in the Grid Hosting redundancy system.  Ultimately, GoDaddy admitted that it was something happening on their end.  Then, finally, someone on the PayPal boards triggered me to look up the protocol for a 302, and I realized the problem.  Then, I used FireBug to track the headers, and sure I enough, I found what I needed to know.  So, I questioned GoDaddy as to why the 302 is necessary.

The reply:

Dear Sir/Madam,

Thank you for contacting Hosting Support.

Your issues with communicating with Paypal are related to the methods which we use
to protect our network. For security reasons, we cannot get into the technical
explanations as to why this configuration will not work.

Please contact us if you have any further issues.

Regards,

Aaron R.

Hosting Support

In other words, they can’t come up with a solution that makes the Grid Hosting system behave like a normal server, and, as I mentioned before, that is supposed to be the appeal of this technology.  Give me a break … temporarily redirecting every file somehow protects their network?  Wow!  I guess I know now how to try to bring down their other hosting services right??

Needless to say, I will not be using Grid Hosting from GoDaddy anymore.  I will next try Media Temple, as it is more matured and I have not read about any such issues.  I doubt GoDaddy cares about losing my $4.99 a month, but I will deter as many people as possible from using their system at all.  Their slow, inefficient, vague technical support is a frustration in itself, and the fact that they can’t get their system to work properly, well that’s just bad business.