I'm using an ajax call to download a php file producing a json string out of an sql query.
So as common, The request is for this .php and in the .php I'll do the query and echo the results as json-encoded string.
The file is about 850Kb (2500 records..), so it take a while to get it. I was searching for a way to reduce the download time. I was thinking of activating some kind of apache compression, just like css or js, but:
don't know if it's a good idea in this case
don't know exactly the htaccess syntax and mime type. And have I to compress a json mime or a php one?
Anyone has already solved this kind of issues?
:)
You should try using ob start with ob_gzhandler before outputting the result in your ajax script. This will compress your output and reduce render time.
ob_start("ob_gzhandler");
Read manual entry here: http://php.net/manual/en/function.ob-gzhandler.php
So my php code would be
<?php
//get all processing done.assuming $json_result holds output.
ob_start("ob_gzhandler");
echo $json_result;
ob_end_flush();
exit;
?>
Make sure that nothing is output to buffer in your script before hand like custom headers or any other prints.
Related
I need to post values to a server. The server will respond with a webpage. I do not want to read the response and echo to a page. I only want to post and let the server display by itself. "header(location:bla bla)" is a "get" I can't used it. With Curl or pear, I will need to explode the response and display it (is a no,no). Any other suggestions?
what about automatically submitting a form (method="POST") with javascript?
Your question is hard for me to parse, although I tried my best and I also edited it for others.
I think what you may be looking for is the following. For example, if you wanted to display a plaintext file directly in the browser:
// Do processing above...
header('Content-Type: text/plain');
echo $text;
exit;
This will have your browser output the data as if it were a text document, not an HTML page.
But, say you want to do something more exotic? Like output a PNG image?
header('Content-Type: image/png');
echo $pngData;
exit;
I hope that helps. Search online for MIME Types to find more of these values for other file types.
I am trying to fetch the meta information from URL results passed after a search. I have been using the OpenGraph library and also PHP's native get_meta_tags function to retrieve the meta tags.
My problem is when I am reading through the contents of a URL that happens to be a .m4v extension. The program tries to read the contents of that file but it is way too large (and not mention, completely useless as it is all junk) and my program refuses to let it go. Therefore, I am stuck till the program throws a timeout error and moves on.
Is there any way to stop reading the contents of the file if it is way too large? I tried file_get_contents() with the maxlen parameter, but it still seems to read through the entire page. How can I quickly determine if a file is structured with tags before I dive in to farm it for meta?
get_headers() is what you need, there's a Content-Type and Content-Length in the response that you might be interested in.
You might want to:
$headers=get_headers($url,1);
Use php's filesize($yourFile); to find the file size in bytes:
$size = filesize($yourFile);
if ($size < 1000) {
$string = file_get_contents($yourFile);
}
I'm trying to echo out an XML File via a PHP File. Like so:
Url visits viewxml.php?id=1
Php open a specific XML file, such as "xmlfile_".$_GET['id'].".xml" and prints it as if they are visiting the xml file itself.
The reason I'm using PHP is to do Session checks to make sure they are allowed to view the xml file.
This is how I am doing this:
header("Content-type: text/xml; charset=utf-8");
// send the saved XML file.
include "xmlfile_".$id.".xml";
Doing this gives me the following GC XML error:
And there is nothing below it.
I have tried different approaches, such as printing it or turning it into a string but then I have problems reading the XML on certain software. I am hoping one of you can help me.
Thanks!
include() will attempt to evaluate the file; if there's something in there that looks like PHP then it will corrupt your results.
Try using readfile instead; this will only output the file contents.
I know that ob_start turns on output buffering, but I don't fully understand what it means. To me it means that it just stops outputting the script data.
Is this true? How does the browser output data in this case, do I have to use ob_end_flush() to turn it off in the end?
Since ob_gzhandler compresses web pages, how do browsers handle these pages?
I have seen ob_start("gzhandler") in code, since ob_gzhandler compresses web pages, what does ob_start("gzhandler") mean and how does it apply to both functions?
All help appreciated!
Output buffering means that instead of writing your output directly to the stdout stream, it is instead written to a buffer.
Then when the script finishes (or when you call ob_end_flush()), the contents of that buffer are written to stdout.
Using ob_gzhandler transforms the contents of the buffer before writing it to stdout, such that it is gzip compressed. (Browsers which support gzip compression reverse this on the opposite end, decompressing the content.)
Ok, let me explain it like this,
It is only one of the uses of the buffer system but I think it's kinda cool.
first I want you to look to this animation.
Operating System Start
When you have a php script that has a level based structure like this, for example you may write:
Connection established to database server..
Database selected : my_database
Data query started
Data query ended (found:200 rows)
...
etc. but if you don't use output buffering and flushing, you will see these lines when all of your script execution ends. But, when the thought is "I want to see what my script is doing when!", you first need to..
Sorry you first need to set implicit_flush to "on" at your php.ini file and restart your apache server to see all of this.
second, you need to open the output buffering (shorthand "ob") by "ob_start();", and then,
place anywhere on your code "echo" statements and after that "ob_flush();" commands to see your script running on realtime.
Later, it is also used for file based static content buffering like this:
place ob_start() at the start of your page (or the start of content you want to capture)
place ob_end_flush() at the end of your page (or the end of content you want to capture);
then $my_var = ob_get_contents(); to get all the HTML output that server creates and sends to the client into my_var variable and then use it as you want. Mostly it's saved to a file and by checking the file's last modification date, it's used as a static buffering.
I hope I could light some bulbs on your mind.
It looks like it does not matter what image headers I use for spitting out via php script an image file with unknown extension, call it .ext
Are there any actual effects? (Is PHP silently converting it, or bypassing the format?)
PHP is most certainly not silently converting anything. It's the browser trying its best to interpret what it got served. Most browsers are likely not even paying that much attention to either the Content-Type header or the extension and just try to identify the file by its content.
Is the output you get the output you expect?
Maybe the script is outputting an error or the function you are using isn't binary safe, I'm guessing from your tags that you are using file_get_contents but what you may be looking for is fpassthru?
Try opening the file in your browser without setting headers or set a standard html/text header, see if something curious is happening.
Good luck.