tags:

views:

64

answers:

3

Im working with a foreign API and im trying to get a fix on what all its sending to my file with the GET method.

How can i output something like print_r($_GET) to a file so i can read what all its sending?

+1  A: 

Writing to a file:

You could use file_put_contents() to create the file with your output. This function will accept a string, or an array, which releases you of the burden to convert your array into a writable-format.

file_put_contents("myget.txt", $_GET);

As stated in the comments, this option isn't ideal for multidimensional arrays. However, the next solution is.

Maintaining format:

If you'd like to maintain the print_r() formatting, simply set the second parameter to true to return the value into a variable rather than outputting it immediately:

$output = print_r($_GET, true);
file_put_contents("myget.txt", $output);
Jonathan Sampson
Mind you though, that this will not write the items in $_GET as seperate lines. It will simply append every item in $_GET after the other without any newline character.
fireeyedboy
The first option does not work if you have nested arrays in $_GET like $_GET = array( 'coord'=>array('x'=>'10', 'y'=>'10'), 'speed'=>'10');
VolkerK
+4  A: 

If you have a hash, both of the listen solutions won't give you the keys. So here's a way to get your output formatted by print_r:

$var = print_r($your_array, 1);
file_put_contents("your_file.txt",$var);

The second option of print_r is boolean, and when set to true, captures the output.

Erik
+1  A: 

It sounds like you need a log to store the $_GET variables being submitted to your script, for debug purposes. I'd do something like this appending values to the end of the file, so the file is not overwritten every request:

file_put_contents('path_to_log.txt', print_r($_GET, true), FILE_APPEND);

pygorex1