Console Style PHP Script Response Pushing and Max Time - php

I realize the title is a bit incomprehensible, but, I am struggle on how to define what it is I am trying to accomplish.
Basically, I am creating my own photo gallery because none of the existing ones met my needs. The one part of the script loads the directory where my photos are located, and adds them to the database, loads the image tags, XMP info, etc. The problem is, it takes way too long. As a challenge, I want to do it without tampering with the max execution time.
I think would would be best is the PHP script sends notifications to the browser into a console style page. Ie, "File 3232 Processed \n, File 3233 Processed". Then using the shutdown function in before the max time, it sends a message saying to the browser that it got shutdown on file 3234 and a javascript function reloads the script starting at file 3234 through ajax or something.
My issue is I do not know how to get the PHP script to send the console style messages instantaneously. I tried various ajax load commands, but it waits for the entire script to finish, then sends a huge glob of results. I have tried the flush command and ob_implicit_flush(1) with no avail.
Links to examples or tutorials, or postings would be great. Thanks

You can do it the other way around: send the full list of images to the browser, and the browser will make ajax requests to process specific image.

Related

How to save an html static page using PHP on Wordpress

I have a wordpress site. In my site there I have this one specific page that shows a lot of content. This content is based on many custom post types. I have built the page by writing a specific php file for that page called patio; i.e. page-patio.php.
The problem is that since the logic is complex it takes the server about 30 seconds to respond, I have optimized images and everything else that loads at the time; but I see that what takes too long is the server response.
I could try to optimize at server level, but I am seeing that it does not make any sense that all that complex logic and database reading should by done every time a user wants to display the page. The data changes once a day, maybe more often in the future.
I want to run a cron that executes a php snippet. Then that PHP snippet would prepare the page, i.e. write the html. So when a user clicks on the page I should just show that static html page and with javascript let him navigate the content.
Anyone found a solution for this?
Good morning.
Is it a server problem, or number of CSS, JS and cookies? Is it a shared server?
Maybe you change your Theme for one that use pure javascript, or a few libraries JS and CSS be better. Be right about problem: if it's with server, or number of libraries and cookies, or both situation. However, if you can't change anything, you can contract a better server service for your application.
I gave up using wordpress to priorize page performance.
I was able to solve my issue very nicely, thanks to this 10 year old post:
Link to Stackoverflow post on saving to html page
I created a cron event that runs periodically that runs a PHP Snippet; I use the Code Snippet plugin to create the event:
if ( ! wp_next_scheduled( 'iKid_cron_twicedaily' ) ) {
wp_schedule_event( time(), 'twicedaily', 'iKid_cron_twicedaily' );
}
add_action('iKid_cron_twicedaily','iKid_Static');
And then the same plugin to create the iKid_Static function.
This function uses the ob_start() and file_put_contents('yourpage.html', ob_get_contents()) commands to write the html page on my server.
And then on my actual page I code:
$content = file_get_contents($cacheFile);
echo $content;
This way my users now get all the information on that page in just 2 seconds, down from about 30 seconds.
I will be able to improve this surely when I move to a better server, but for now that is a great improvement for me.

PHP - Edit Request Being Made by Other Request to the Server?

everyone.
I'm working with cURL to download some page from a website. After the webpage loads, it makes a few other requests to the server to get some files from the server to load into the HTML (different images I want to compare that are randomized - they're a few decorating images that are randomizes, if that matters - a few are displayed, selected randomly from a set of images).
I want to load the images for the comparisons via cURL, but only after the page finished downloading.
Right now, in the webpage I get from the request I made, I get <img>s, but their srcs are wrong, because they're stored locally on the server I'm fetching the webpage from.
Because of that, the page on the original page on the website is able to get the picture (since it's running on that server), yet I'm unable to get them successfully.
I already have the page in as a string variable. I know the address of the page and the URL/paths of the images, but I don't want to make another request to the server, because then the images would be randomized, and I want to do the comparisons between then after the page loads.
How can I do that? Is it possible to make a a request? Is it possible to somehow change my existing request of the page, or modify anything else that would bring me the images?
I know how to make requests, but I've never dealt with this kind of a "depth" of requests.
Thanks a lot in advance!

How to load the entire (or more of the) DOM for inifinite scrolling page that uses Ajax?

If you look at a page like this: http://www.fieg.nl/ias-demo#/
You can see that it uses Ajax to dynamically add content when you scroll to the bottom, similar to how Google Images works.
If there was a page like this that I wanted to capture for parsing, I would do something like:
$page = file_get_contents("http://www.fieg.nl/ias-demo#/");
But this only gets everything that initially loads before any Ajax happens. Is it possible to use PHP or CURL or any other programs to capture the entire page and automatically load the Ajax content and capture that as well?
Also if there happens to be a weird page where it never stops loading things and literally goes on forever, I'm not sure how the tool would have to handle that. Because the tool would never find the end of the DOM in that situation.
Those pages work by sending an ajax request to fetch more data as the user scrolls towards the bottom of a page. The javascript then writes the response of the ajax request into the bottom of the page.
You need to run a tool like firebug to analyse the requests that are made to the server to retrieve the next page of content (using the net panel). Once you have found out the request URL, you need to emulate these requests in your PHP script.
Unfortunately SO is not the place for people to write your scripts to spider websites, but there is the theory anyway.

Delay for Simple HTML DOM Class

I'm using the simple html dom class and have gotten it to work on basic pages and can view the information I want. However, when I attempt to use it on a page that reloads a div with ajax, I can't seem to get it to "wait" before reading the page.
I basically want it to load the page, then wait 2 seconds before reading the page content (so that the new div has time to load). Is this possible or am I trying to use the class incorrectly? I'm manually inputting the URL, so it's not a link issue.
Example Page:
- You can see the load issue when you navigate through the pages.
Someone suggested curl and I tried that with the same results.
Thanks in advance.
PHP runs on the server. JavaScript (e.g. AJAX) runs in the browser, after the PHP code on the server has finished producing the page. You can't make a PHP program, running on the server, wait for an event that happens later in the browser.
You'll need to either load the content for that div using PHP code, or replace the PHP DOM-parsing code with JavaScript code that does the work on the client.
You can use the Sleep method ( http://php.net/manual/en/function.sleep.php ) if you simply want to delay the program execution for some set amount of time.

Trying to display a Loading screen in PHP, but doesn't display until after script is executed?

I am trying to display a loading message with an loading bar animation so people know the script is working away.
However even though the HTML is at the top of my PHP script , it only gets displayed once the script is fully loaded.
The script takes about 40 seconds to complete because it Generates a PDF, Inserts Data into a DB then sends a MIME email to an external MX (takes the longest part).
Here is the HTML that displays the loading message:
<div class="grid_16">
<fieldset>
<h2>
Processing <img src="img/loading.gif" alt="loading animation">
</h2>
</fieldset>
</div>
<?php
//rest of php page
Any idea how I display this out to the user so they don't get impatient and start hitting F5 etc.
Thanks
Merry Christmas!
PHP loads on the server and is then passed to the browser. This means that, until the page has completely finished loading on the server it will not be passed to the browser for display.
What you will need to do is use javascript and ajax. Firstly, remove the slow PHP from the HTML and show the page. Then once that has shown load the PHP script via ajax and once it has completed write it to the document.
My friend, PHP only sends output when it finishes script interpretation(1). So, your option is to put the gif in the preceding page fired (using some JavaScript) with the event that moves the user from oldpage.php to slownewpage.php. So, the gif will stay in front of user until PHP sends the new HTML result and the browser has something to work with.
(1) Unless you order him to do otherwise with something like ob_flush
run flush(); after the part you want to directly output (within php), because you are probably buffered especially if you are gzipping your code. You can load the page like others said and pull the result with javascript as well (actually that's probably the better option).
.. can I ask why you are sending a long message during a page load? Can't you have a cron job do it (check every few seconds to see if something needs to be sent)? I think they have cron programs for windows as well.

Categories