Search code examples
javascriptphphtmlhtml-parsing

Is it possible to run a PHP file with HTML on the server


Normally when you have a .PHP file and the client request it, the PHP code is run on the server and the HTML and JavaScript are sent to the client.

Question

Is it possible to have the server request a webpage (local) and run both the PHP code and the HTML with JavaScript on the server? I have created a single .html file that after 3 seconds of processing locally creates the image data for a thumbnail of the given video.

Why

I need to generate a thumbnail for a video. I used shared hosting and my hosting provider doesn't support for ffmpeg. You can, however, generate thumbnails using a canvas and JavaScript. I have already put a lot of pressure on the client. If this is possible, upload and download times would be significantly shorter than using the client.

Attempts

I've tried using file_get_contents(), but it doesn't run the code (Makes sense). Is there a way I could have it open and run for x seconds and then grab the contents?

I've tried using curl to get the file using this function here. I believe it is similar to my previous attempt in that it gets the file contents, but never executes them.

My final attempt was to use new DOMDocument(). I couldn't even get to loading the page though. First, I can't parse it with a video tag. It gives this error:

Warning: DOMDocument::load(): Specification mandates value for attribute controls in 
file:\path\to\html\document.html, line: 53 in C:\path\to\php\document.php on line 50

If I were to remove the video tag (which is required), I get errors while parsing my JavaScript. So that attempt also did not work.


Is there a way that I could have PHP process the code (for something on the server) for x seconds before getting the contents? It would allow for time to generate the thumbnail data. If there is another way to do this without using ffmpeg on the server, that would be great.


Solution

  • So as I mentioned in comments, what I'm gonna explain is just an option (not the best one and just answering for your need of running html code!)

    Where to do this?

    Personally I rather to do this when the video is being uploaded by admin's browser and the best thing is that you can do this as a part of the posting procedure.
    So in the page that you want this process to be done, put an invisible iframe like this.

    <iframe id="myIframe" style="display: none;"></iframe>
    

    How to begin the process?

    I don't know the way you use to upload the videos (and it really is not that important!) but let's assume you want to use formdata. After the video is uploaded you need to know something unique to address the video (let's say an id). So after the video is uploaded, we can recive a code like id:20, initiateThumbnail:true as the result json data. Then we can simply use that hidden iframe to be the browser you've been asking for like this:

    $("#myIframe").attr("src","dothething.php?video=20");
    

    Now do what ever you wanted to do in it and change it's content after it's done. Now you need to wait for the result!

    $('#myIframe').load(()=>{
        let result = $("#myIframe").contents();
        // checking result!
    });
    

    As you have already thought about, you can handle any errors by processing the result.

    Notes

    1. The event listener we used for iframe (iframe.load) fires when you initiate making the thumbnail as well. So be careful with the process of checking result (content of that iframe!)
    2. If you don't use ajax or formdata, simply the action of your form is what I used as iframe.
    3. One question? What happens if network connection goes down during this process? Simple answer! You can check in so many ways that the thumbnail exists or not. If not you can create it once that user requests for it in his browser and upload it back to server and save it for ever (as you did it in admin's panel!)