Putting data from HTTP POST in a CSV with PHP? - php

I'm writing a basic android app that sends GPS data via HTTP POST to a PHP page. I want the data to just be the two values (latitude and longitude) separated by a comma.
<?php
$myFile = "requestslog.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "\r\n");
fwrite($fh, file_get_contents('php://input'));
fclose($fh);
echo "<html><head /><body><iframe src=\"$myFile\" style=\"height:100%; width:100%;\"></iframe></body></html>"
?>
The text file shows the data like this:
lat=55.020383&lon=-7.1819687
lat=55.020383&lon=-7.1819687
lat=55.020383&lon=-7.1819687
lat=55.0203604&lon=-7.1819732
lat=55.0203604&lon=-7.1819732
lat=55.0203604&lon=-7.1819732
Is it possible for the PHP to replace the '&' with a ','? I'm lookg for the end result to be a csv with those 2 rows. I don't have an experience with PHP and any help would be great.

str_replace('&',',') should do the trick.
If you want to end with just values, you can do following on every line:
str_replace( array( 'lat=', '&long=' ), array( '', ',' ), $sLine );
where $sLine is line from your file.
Complete example:
<?php
$myFile = "requestslog.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "\r\n");
fwrite($fh, str_replace( array( 'lat=', '&lon=' ), array( '', ',' ), file_get_contents('php://input')));
fclose($fh);
echo "<html><head /><body><iframe src=\"$myFile\" style=\"height:100%; width:100%;\"></iframe></body></html>"
?>

Related

How break a line of code [duplicate]

This question already has answers here:
Writing a new line to file in PHP (line feed)
(4 answers)
Closed 9 years ago.
I am trying to break the line after this code. I can't figure it out what ever I do the output of my code is just as shown below:
Output:
User: adminLogged in: 2014-02-09 05:34:30User: adminLogged OUT:
2014-02-09 05:34:36User: tataLogged in: 2014-02-09 05:34:41User:
tataLogged OUT: 2014-02-09 05:34:43
I want to set some space and new line.
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username";
fwrite($fh, "$stringData");
$stringData = "Logged in: $date";
fwrite($fh, "$stringData");
fclose($fh);
Just make use of \n or \r in front of your fwrite().
Like this.
fwrite($fh, "\n$stringData");
^-------- // Add this to both of your fwrite() calls
Try this
$date = date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username";
fwrite($fh, "$stringData\n");
$stringData = "Logged in: $date";
fwrite($fh, "$stringData\n");
fclose($fh);
Here you just add \n before or after variable assign just like this fwrite($fh, "$stringData\n"); or fwrite($fh, "\n$stringData");.
For more details, see this question.

Receiving post data xml

Hi I need to receive a post data which will be in an xml that is encoded in a base64 format.
I'll be receiving this from a payment gateway. Now all i get is this. My code creates a txt file but is empty. Is there anything wrong with the code? The output should be an xml envelope in a text file.
$body = '';
$fh = #fopen('php://input', 'r');
if ($fh)
{
while (!feof($fh))
{
$s = fread($fh, 1024);
echo $s;
if (is_string($s))
{
$body .= $s;
}
}
fclose($fh);
}
$body = base64_decode($body);
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $body;
fwrite($fh, $stringData);
fclose($fh);
I tried to contact the payment gateway and they are telling me that they are getting this error "The remote server returned an error: (417) Expectation failed." where could the problem exist us or them?
Since your file is returning blank, I would recommend verifying the specifications from the payment gateway for your fopen() function. In addition, if you are properly getting data back from them, then I would check the base64_decode() function. I have seen situations where there may be a header or other data at the top of the actual payload data that fouls up the base64_decode and ruins your day.
It looks like you are mixing up your file handlers, can you see if the following codes run:
$body = $HTTP_RAW_POST_DATA;
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$stringData = $body;
fwrite($ourFileHandle, $stringData);
fclose($ourFileHandle);
I have just tested the above code myself successfully.
What I suggest you do is trying the following:
Try to put a static piece of data into $body (eg: "$body = 'test';")- and see if that saves - if not then it is an issue at your end.
Delete the file (to remove any permission issues).
Double check the url that the payment gateway is sending to is correct.

Writing to Text files via php

I have a little issue while trying to kill time. I am writint to a text file the contents of the filled in form so it is easy to read. At the moment everything gets put int a single line, and therfore I can not tell where msgs begin or end. I wrote a php like this:
$from = $_POST[from];
$friend = $_POST[friend];
$carrier = $_POST[carrier];
$message = stripslashes($_POST[message]);
if ((empty($from)) || (empty($friend)) || (empty($message))) {
header ("Location: sms_error.php");
}
else if ($carrier == "orange_mobile_network") {
$formatted_number = $friend."#orange_mobile_network.co.uk";
mail("$formatted_number", "SMS", "$message");
header ("Location: sms_success.php");
}
Then the SMS/Text message will be sent. After this I wanted to write/store/append the message on a txt file. So I wrote:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $_POST[from];
fwrite($fh, $stringData);
$stringData = $_POST[friend];
fwrite($fh, $stringData);
$stringData = $_POST[message];
fwrite($fh, $stringData);
fclose($fh);
But like I said. Everything works. I just want to be able to read the file and put everything in a new line and possibly format it nicely for easy reading. I do not want to use a DB for storing the TXT as my host is going to charge me.
change this:
fwrite($fh, $stringData);
with this:
fwrite($fh, $stringData . PHP_EOL);
or:
fwrite($fh, $stringData . "\r\n");
You can append PHP_EOL when you write for a platform aware newline. I.e. $stringData = <string> . PHP_EOL;
If don't need it to be platform aware, going with the windows newline is a safe option.
define('CRLF', "\r\n");
$stringData = <string> . CRLF;
fwrite($fh, $stringData);
I do not know exactly Cause you're using. txt instead of a bank, but my tip for this specific case is to work with json format.
Example:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
//assembles a string of type json
$data = json_encode(array(
'from' => $_POST[from],
'friend' => $_POST[friend],
'message' => $_POST[message]
));
//Write in the file
fwrite($fh, $data);
fclose($fh);
//reads the file
$content = file_get_contents($myFile);
$object = json_decode($content);
var_dump($object);
good it is at least better organized.
a hug!

Read XML data and write to new file with PHP

I may be over thinking this but are there any extra steps I am missing after reading my XML data into a string and before writing it to a local file?
header('Content-type: text/xml');
//MY URL TO A XML PAGE STYLED WITH XML STYLE SHEET
$url = "http://www.mywebsite.com/somexmlfile.xml";
//SAVE CONTENTS OF PAGE IN XML_DATA
$xml_data = file_get_contents($url);
//REMOVE STYLE SHEET INFO
$search2 = '<?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?>';
$xml_data = str_replace( $search2, "", $xml_data);
//WRITE MY DATA TO A BACKUP FILE AND THEN ECHO TO THE SCREEN FOR A FLASH APP CALLING THE INFO
$myFile = "data_backup.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xml_data;
fwrite($fh, $stringData);
fclose($fh);
echo $xml_data;
The reason I ask this is when I try opening my backup XML file in Dreamweaver it crashes everytime. I was thinking there could be an encoding issue possible.

explode is not working to split string

we unable to split the string following code.please Help us.
<?php
$i=0;
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "no\t";
fwrite($fh, $stringData);
$stringData = "username \t";
fwrite($fh, $stringData);
$stringData ="password \t";
fwrite ($fh,$stringData);
$newline ="\r\n";
fwrite ($fh,$newline);
$stringData1 = "1\t";
fwrite($fh, $stringData1);
$stringData1 = "srinivas \t";
fwrite($fh, $stringData1);
$stringData1 ="malayappa \t";
fwrite ($fh,$stringData1);
fclose($fh);
?>
$fh = fopen("testFile.txt", "r");
$
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
}
fclose($fh);
$Beatles = array('pmm','malayappa','sreenivas','PHP');
for($i=0;$i<count($Beatles);$i++)
{
if($i==2)
{
echo $Beatles[$i-1];
echo $Beatles[$i-2];
}
}
$pass_ar=array();
$fh = fopen("testFile.txt", "r");
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
$t1=explode(" ",$line);
print_r($t1);
array_push($pass_ar,t1);
}
fclose($fh);
If i read the code correctly you are writing the string seperated by \t but try to explode with spaces, use:
explode("\t", $string);
You could use fgetcsv, since you're just doing a standard tab-delimited input file. Given your sample file of:
no [tab] username [tab] password
1 [tab] srinivas [tab] malayappa
then
$lines = array();
$fh = fopen('testfile.txt', 'rb') or die ("can't open testfile.txt");
while($lines[] = fgetcsv($fh, 0, "\t") { // no line length limit, tab delimiter)
...
}
will give you
$lines = Array(
0 => Array(
0 => 'no ',
1 => 'username ',
2 => 'password '
),
1 => Array(
0 => 1,
1 => 'srinivas ',
2 => 'malayappa'
)
);
You're exploding on whitespace. Unless there is whitespace in the string your exploding then no, it won't work.
Try using the code markup to make your code a little more readable in order to get better quality responses from people.

Categories