How to send DOM tree (including HTML, CSS, javascript) from a Php server to a client?
Thanks.
EDIT: As I read the question again, I see that you're interested in concealing the source code, essentially. Being that this is a game, you will likely have tons of javascript/jQuery. If this is what you mean, you should research obfuscating javascript
Original Response:
I understand your question to ask whether or not you can "create" an "entire" page on the server, by running a combination of php, js, jquery, etc. If this is a correct interpretation, then the short answer is no. If it all is to be done server-side, you'd be better off using a server-side scripting language exclusively.
On a related note, you could dynamically generate the jQuery code on the server before sending to the client. I'm not sure that's what you're asking though, because the entire web is composed of pages that are created then sent to the user-side...
Related
I am currently trying to load an HTML page via cURL. I can retrieve the HTML content, but part is loaded later via scripting (AJAX POST). I can not recover the HTML part (this is a table).
Is it possible to load a page entirely?
Thank you for your answers
No, you cannot do this.
CURL does nothing more than download a file from a URL -- it doesn't care whether it's HTML, Javascript, and image, a spreadsheet, or any other arbitrary data; it just downloads. It doesn't run anything or parse anything or display anything, it just downloads.
You are asking for something more than that. You need to download, parse the result as HTML, then run some Javascript that downloads something else, then run more Javascript that parses that result into more HTML and inserts it into the original HTML.
What you're basically looking for is a full-blown web browser, not CURL.
Since your goal involves "running some Javascript code", it should be fairly clear that it is not acheivable without having a Javascript interpreter available. This means that it is obviously not going to work inside of a PHP program (*). You're going to need to move beyond PHP. You're going to need a browser.
The solution I'd suggest is to use a very specialised browser called PhantomJS. This is actually a full Webkit browser, but without a user interface. It's specifically designed for automated testing of websites and other similar tasks. Your requirement fits it pretty well: write a script to get PhantomJS to open your URL, wait for the table to finish rendering, and grab the finished HTML code.
You'll need to install PhantomJS on your server, and then use a library like this one to control it from your PHP code.
I hope that helps.
(*) yes, I'm aware of the PHP extension that provides a JS interpreter inside of PHP, and it would provide a way to solve the problem, but it's experimental, unfinished, would be still difficult to implement as a solution, and I don't think it's a particularly good idea anyway, so let's not consider it for the purposes of this answer.
No, the only way you can do that is if you make a separate curl request to ajax request and put the two results together afterwards.
Now before you get out the torches and rail against spammers, I'll explain my intent here. I have written a series of scripts which scrape a certain website for contact information. These contacts are highly focused and are likely in a position where they are in need of a specific service I offer. The messages I plan on sending to them are one-offs and are written to be very helpful and respectful.
Now having said that, I'm having a hard time finding information on how to write a PHP bot that can enter a website, access a form, and send it. Everything I find is about stopping "spambots", unsurprisingly. I'm not worried about duping recaptchas or anything like that. If they have measures like that in place, I'm fine skipping them.
This question is too broad, so I have to give you a broad answer too...
First you need to download the page. You can use cURL (or file_get_contents might sufice).
Then you need to parse it with an HTML parser. You can use DOMDocument that comes bundled with PHP but you'll probably choke since DOMDocument is not very forgiving about pages with HTML syntax errors (or HTML5, for that matter)
Then you need to traverse the DOM and look for the form itself, extract the url and the method and make a request.
You can then use cURL to send a submit request to that url.
However, this will fail for dynamic pages (for instance, angular and other heavy javascripted pages). You probably better to use a headless browser like phantomjs.
I just started writing server code (php), I finished the client side with javascript and html. I have some questions that I hope to get some clarification on before I start coding the server side.
Is it possible/a good idea to create DOM elements (img, p, header etc) on the server side (php), send it to the client side via AJAX, client side set some extra attribute and 'attach' it (the received DOM) to a div on that html page?
I want this because those DOM elements will be used on multiple pages.
Is it possible, and if possible, is sending the entire DOM efficient?
When sending raw HTML rather than JSON you want to consider things like:
the size of the request is going to be bigger. JSON is much more succinct
the time to render the element is going to be shorter (no need to parse the JSON and render the HTML, you're already done it server side)
reusability and caching is pretty easy. Indeed, The same snippet could be served to multiple clients and the template can be cached (the same is true if you used precompiled template in JS, but it's slightly more complex).
Code logic is all in one place (server side) or in two places (JS and server side).
The recommended way to accomplish what you want is using a Template Engine, which out there are plenty of them.
This way you can have separate files for the templates and load them as necessary via an AJAX request.
I recommend you the use of Handlebars, its pretty simple and you can use it with JavaScript and also with PHP itself!!!
See the PHP Handlebars port here: https://github.com/XaminProject/handlebars.php
I think topic ask the question, I usually use PHP for parse/ web scraping, but I have really bad time scraping javascript most cases I cant do it
ex: Parse a div that appears when a javascript its executed.
I readed about RUBY, that have a parser library for javascript, so question is w is the languaje for program a web scraping that will effective scrap javascript generated content ?? Its here a library for PHP like the one for ruby for parse javascript content ?
There are a handful of strategies for this. Depending on your needs, consider pro grammatically instantiating a browser instance that you can hook into and read the page from.
The idea is, let the browser do the work, as the page is made for a browser and not your bot. You can then tap in and scrape away using a browser plugin that feeds data to your primary application running things.
This may be way overkill for what you need though. I'll leave it up to you to decide.
You should look at some GUI-less/headless browsers. There is some written for Java. I didn't find one for PHP.
Look at :
HTMLUnit
Golf
You can try using something like Selenium, which allows you to automate browser tasks.
On the other hand, you can go into details on what happens when the js code is executed. For example, if the js code is requesting something from the server by POSTing some data, you could emulate that in the regular fashion.
You should look at PhantomJS and CasperJS (headless browsers).
In the ruby world the gem for running Phantomjs would be poltergeist
There is another article about some of the options you have in ruby here too (however they are not all js capable)
I have been doing some research on mobile websites, and thus far it seems to me that PHP is not supported by most handsets.
I am trying to create multiple form's that lead up to a auto email response, which would be fairly simple if I could use PHP.
Is there any other way of achieving this?
PHP is server side language, your server has to be support for it. handsets should support html or whatever you output with php.
PHP is usually executed on the server, not the handset itself, so the question is more if your PHP spits out HTML unsuited for a handset.
Yes - its perfectly possible to write WML using PHP, only vaveat is that you need to add a custom header to set the mimetype if your default behaviour for PHP files is to return text/html. And, of course you need to understand WML - which is quite different from HTML.
These days a lot of handhelds can now cope with HTML although don't expect javascript.
PHP is not supported by most handsets
Eh? What's that got to do with it? PHP runs on the server - not on the client.
C.
Have a read up on the basic request/response cycle for a web page.
Assuming by "mobile website" you mean "a normal website that is tailored for display on a mobile device" the answer is that PHP runs on the server not the client, the client only displays HTML given to it by the server.