0

My first post so please be kind, or not if thats how it works around here. I have a simple script making an API call to retrieve the data of an error log, and using the $_GET method to call the url inside the script as I plan in the future to run this as part of a bigger script that will run via a cron.

The problem I am having is when the data is displayed it is just a huge mess of a wall of text not formatted in the slightest way. Each line in the error log begins with a date, and time.

Here is a small sample of the output.

{"type":"success","response":{"data":{"thispage":1,"totalpages":1,"log":"2015-03-09 08:16:28\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>Title of Stuff - Book Stuff\n<\/TIT2><\/metadata>]\n2015-03-09 08:20:01\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>Another TItle of Stuff - Music Title\n<\/TIT2><\/metadata>]\n2015-03-09 08:25:40\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>Holy Cow - Title of CD\n<\/TIT2><\/metadata>]\n2015-03-09 08:31:33\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>The pizza king - eat pizza\n<\/TIT2><\/metadata>]\n2015-03-09 08:35:56\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>Ouch - Ouch Ouch\n<\/TIT2><\/metadata>]\n2015-03-09 08:40:19\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>What shall I do - Dump\n<\/TIT2><\/metadata>]\n2015-03-09 08:45:59\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>Its not a hangover - its cancer\n<\/TIT2><\/metadata>]\n2015-03-09 08:49:31\tINFO\t[ADMINCGI] XML title update [<?xml version=\"1.0\" encoding=\"UTF-8\" ?><metadata><TIT2>something is going on here - lalala\n<\/TIT2><\/metadata>]\n

But of course it is in a huge wall of many lines of that. How do I parse that so that each line begins with the date and time of each new item?

Here is the php code with a sprinkle of javascript I am using, that results in the above.

    <script>
    javascript:void(location.href="http://url:port/api.php?xm=server.readlogs&f=json&a[username]=pizzaman&a[password]=THEpassword&a[type]=error");
</script>
<?php
    $output = $_GET["a[type]"];
       echo $output;
?>

Thanks in advance for everyones help

JMiller
  • 99
  • 11
  • The output from your API should read the file and create a list of lines. `{ "log": [ "line1", "line2", "line3" ] }` – Matt Mar 09 '15 at 21:29
  • I found this post that might be all you need. http://stackoverflow.com/a/9120871/378352 – dudeman Mar 09 '15 at 21:30
  • I think I need to clear some confusion possibly...The error log is at the moment displaying XML inside the log, it is not natively XML. – JMiller Mar 09 '15 at 21:49
  • If you are asking the api to return JSON data, then you need to create a valid json string to return to the called.. So read your logs and create a valid json string to return – RiggsFolly Mar 09 '15 at 23:48

1 Answers1

1

I figured out a different way to do it using file_get_contents. I must have not been to clear with my question as this was staring me in my face for awhile.

<?php
header('Content-Type: text/html; charset=utf-8');
$homepage = file_get_contents('http://url/api.php?xm=server.getlogs&f=json&a[username]=pizzaman&a[password]=PizzaPassword&a[type]=error');
echo"<pre>". str_replace('\n', '<br>', $homepage) ."</pre>";   
?>

This then takes all of the /n lines and turns them into line breaks, which makes everything look semi-pretty. Now I just need to figure out how to replace all of the /t with a blank space and I am all set.

If anyone has a better solution, or a way to make it cleaner I am all ears! Thanks everyone.

JMiller
  • 99
  • 11