Instead of storing the data in tables we have stored them in log files in server. I want to create reports on these data. I thought of connecting to the server and getting those files parse them and run the queries on them but my manager asked me to write script on the server which does the query calculation and return the data set back.
How can I accomplish this. I am new to writing scripts on a server. Can I write PHP script on the server? Can someone please explain how to accomplish the above task?
Can I write PHP script on the server?
Yes
Can someone please explain how to accomplish the above task?
This is way to broad to do in an answer at StackOverflow. You'll need some programming skills to do this and if you're unsure of where to begin then you probably don't have them and should hire a developer to do this for you.
But the steps would be:
Write a script that parses those files and loads them into a database so you can run your queries efficiently. Don't reinvent the wheel.
I would write it as a PHP script, like you said. Write the PHP script to deliver the results in a standardized format, say, XML or JSON. Then, your client program would simply download from, say, http://example.com/secure/processLogs.php. Depending on your client language of choice, you can use several methods of authentication, such as HTAccess, to secure the PHP script. Once you have the JSON or XML downloaded, just run it through a parser (just Google for it, they're a dime-a-dozen), and bam, you have your results loaded into the client. Hope this helps.
here is a little example:
// ---- begin of client.php ---
//
// client sending request to server responding with JSON
//
<?php
function connectToServer($server_url,$post_data) {
$ch = curl_init($server_url);
if ($ch == FALSE) return false;
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
if (curl_errno($ch) != 0) {
$errstr = curl_error($ch);
curl_close($ch);
return false;
} else {
curl_getinfo($ch, CURLINFO_HEADER_OUT);
curl_close($ch);
return json_decode($res);
}
}
// sending data to server, to be processed there and waiting data from it:
foreach(connectToServer("http://192.168.0.1/server.php",
array("my"=>"data")) as $key=>$val) {
printf("%s, %s
",$key,$val);
}
?>
// ---- end of client.php ---
// ---- begin of server.php ---------
<?php
if(isset($_POST["my"])){
$my = $_POST["my"];
header("Content-type: application/json");
echo json_encode(array("hi"=>"there","my_is"=>$my));
}
?>
// ---- end of server.php ---------