0

I have a function that receive a $_POST (your content: a variable 'xml' in a xml format).

I've tried this:

function call($_POST) {    
    $xml = $_POST['xml'];    
    $doc = simplexml_load_string($xml); 
}

But when i try write the final variable in a file, nothing is written. (And i've already tried treat the post as an array, but doesn't work too).

Thanks,

  • 3
    Don't pass $_POST as a function argument. It's a superglobal, and is automatically available in ALL scopes within PHP. Have you tried any basic debugging, e.g. `var_dump($_POST)` inside that function to see what you're actually getting? simplexml isn't going to throw a hissy fit because it's inside a function. – Marc B Jul 02 '13 at 15:10
  • how are you calling this function? $_POST is already available and shouldnt need to be passed as an argument. – immulatin Jul 02 '13 at 15:11
  • @MarcB I've already debugging using var_dump. Thanks for help, the problem was in the encoding of post's content. – Talita Bac Jul 03 '13 at 10:34
  • possible duplicate of [passing POST array to php function](http://stackoverflow.com/questions/1660912/passing-post-array-to-php-function) – hakre Jul 09 '13 at 07:12
  • possible duplicate of [PHP: $_GET and $_POST in functions?](http://stackoverflow.com/q/1354691/367456) – hakre Jul 09 '13 at 07:26

2 Answers2

1

I think by declaring $_POST as an input parameter of the function, you've redefined it in the local scope and lost the global data because you try to re-assing it ($_POST is a superglobal). For what you've done, some good existing Q&A material are:

Even those are older, the information they give about the topic are still valid in PHP because those superglobals haven't changed meanings over the years.

However in all current stable PHP Versions (that is PHP 5.4 and up) your code produces a fatal error (Online Demo in 80+ PHP Versions):

Fatal error: Cannot re-assign auto-global variable _POST

This might explain that you don't get any output. See Nothing is seen. The page is empty and white. (also known as White Page or Screen Of Death) in the Reference - What does this error mean in PHP?. You might not see any error message because you don't display it or you don't look into the log-file, see How to get useful error messages in PHP?.

So what can you do? Next to understanding the basics of PHP error handling - and I suggest you to enable error logging and locate the error log to get fluent with it - you need to change the code as outlined in the two reference questions linked above.

Either get rid of $_POST as an input to the function, or change the name to something else like $postData.

function call(array $postData) 
{
    $xml = $postData['xml'];
    $doc = simplexml_load_string($xml); 
}

The just call the function with:

call($_POST);

Or if you only pass some little parameters with that array, write them out:

function call($xmlString) 
{
    $doc = simplexml_load_string($xmlString); 
}

Usage:

call($_POST['xml']);

This will allow you to use different form names with the same code. Very useful because you don't tie things too hard together but leave some wiggling room.

Community
  • 1
  • 1
Adam
  • 772
  • 3
  • 10
  • @Neutice: I turned your answer into a book hope you don't find this harsh. See as well this one: http://3v4l.org/aiYXR I think you might be interested. – hakre Jul 09 '13 at 08:21
  • @hakre: No worries. Better answers are always... you know... better. – Adam Oct 01 '13 at 22:22
1

God knows how reassigning $_POST as a function argument name will screw things up. Try changing your code to:

function call($xmlstr) {    
    $doc = simplexml_load_string($xmlstr);
    // should probably be a return here, but I'm guessing
    // your function does more than just parse XML...
}

call($_POST['xml']);

Or simply:

function call() {
    $doc = simplexml_load_string($_POST['xml']);
}

call();
Sammitch
  • 30,782
  • 7
  • 50
  • 77