Wednesday, July 22, 2009

HTTP POST Method: Finding the data on the receiving end

This is regarding FINDING the POSTed data with a PHP script. Where is it? Why do I keep coming up with NULL values when I look where it is supposed to be? Well, it depends on how the data was posted, and if some other site is posting the data you really don't know which method they will use. Here are the first three places to look:

print_r($_POST);

if(!isset($HTTP_RAW_POST_DATA)) $rawpost = file_get_contents( 'php://input' );

echo $GLOBALS['HTTP_RAW_POST_DATA'];

There are more places to hunt, but so far these three have worked for me.

PS With the second method above, when receiving XML, you end up with \" for every ", which can mess you up. Try following up with

$cleanpost = str_replace( '\"', '"', $rawpost );

Monday, July 20, 2009

My favorite PHP Class: SimpleXMLElement

1. A lot effort has been expended by countless people to convert XML into PHP arrays. There are countless forum discussions on the subject and many complex scripts to accomplish this task.

But there is something better.

$xml = simplexml_load_string($str);

One line of code converts an XML string to an object of class SimpleXMLElement. Then you can access any individual data element within the object simply by calling the surrounding tags (skipping the outermost [message] tags):

$xml->body->layer1->layer2->layer3

2. It is very useful for debugging objects to have a way of examining the object structure, datatypes, and content. The very useful function for this is var_dump($obj); which lays everything out for you (kind of like print_r($arr); for arrays).

3. Another strange discovery I have made is that instead of enclosing lenghty XML strings in quotes or double quotes, it is possible to dispense with the quotes altogether and enclose it with XML preceded by three "less-than" signs on one end and XML on the other end. I am not sure why this is so or what the ramifications are of this, but it looks like a very useful option.

Monday, July 6, 2009

PHP Shortcut of the Day

Normally in PHP you concatenate two strings like this:

$str1 = $str1.$str2;

This adds $str2 to the end of $str1. Here's a handy shortcut, which when constructing really huge XML strings is highly useful:

$str1 .= $str2;

The "dot-equals" operation is even smart and will let you concatenate to a null string at the beginning, which is a good thing when you are in loops. You can't do that in C ; you have to StrCopy and then StrCat. To me, this type of thing is what makes PHP great.

Here's another great PHP shortcut: the 'foreach' loop.

A foreach loop lets you loop through all the elements of an array and get access to either the individual keys and individual data elements. The syntax is:

foreach( $somearray as $key => $data ) { .... }

For multidimensional arrays you can nest foreach loops. It takes some getting used to this funny syntax, but it is very useful and worth knowing about.

HTML - JavaScript : Button Inputs as Links

Normally Button inputs in HTML post data to a destination link specified when the form is declared. Wouldn't it be nice to have multiple buttons that can post to other URL's on the same form? Most "solutions" in forums and books say create a different form for each button, but that's cumbersome and messes up your page layout. And you are not allowed to "nest" forms in HTML.

Enter JavaScript. The one line solution (for GET method): While declaring your button input include:

onClick="window.location.href='../folder/url'"

for POST method dynamically change the form "action" attribute with:

onClick="document.myform.action='../folder/url'; document.myform.submit();"

This last solution is my favorite because you can have a whole bunch of data in input fields automatically posted to whatever URL you want from just one form.