Access console logs from php's file_get_content() function - javascript

So I have a simple php file with the following code in it:
$contents = file_get_contents("path/to/file.html");
var_dump($contents);
Within the html file I have a few scripts that run and returns data to console.log. But, when i execute the php command (linux virtual machine) and run the file it just returns the static html content. Whereas when I open the file from the browser, it executes all the javascript files and returns the expected html output with data in console.log. To be specific, the html file executes a jasmine test suite.
So, I would like to know if there is any way to print out the results from the console.log or actually execute the javascript files through a php script and a linux virtual machine. Also, the main purpose is to execute the test via Bamboo, a continuous integration server.
I have tried phantomjs but it results in other errors and I would prefer not to use it.

Simple saying, no, you cannot do that.
The reason is that you need browser to execute javascript, and PHP just does not have browser capable of doing that as efficient as you want. There is CURL, but it cannot run javascript.

Related

How to Run Javascript code/script by itself like php bots?

i am reading websocket data in javascript and doing further procedures like inserting data in mysql database.
but all this happens when i open .html page containing javascript code in it.
How can i run the javascript program/code all by itself from lets say command line ?
like the php codes/programmes/bot does , invoking from shell and it can keep doing what its been coded to do, without stopping.
so i can set it as a cron job and it will do its thing on time, instead of me opening .html file every time.
This is exactly what node.js is for. Instead of putting a <script> tag in an HTML file, you would just run your JS file ala $ node myscript.js.
Installe nodejs and you can run .js files by
$ node myscript.js
if you want your script to run forever then you can use https://www.npmjs.com/package/forever like this
$ forever start myscript.js

How do I get the DOMDocument to process in PHP post-javascript?

I am writing a program to search tags, however I can't get these if they are created with javascript.
There is no direct way to do this when it comes to Javascript because it needs a browser to be executed.
Possible Solution
You can run a headless browser with Selenium for example and wait for your DOM elements to be created then - and using Javascript - make a request to your PHP server with the window.document.querySelector('html') data that you want to parse.

Bash/PHP/Javascript: How to run a php file that outputs javascript, and execute that javascript? [duplicate]

I have php file that is to be executed as cronjob, this php file contains some javascript.
I will explain the flow :
the Php is used to retrive some data(A LIST OF URLS) from DB.
For each URL Obtained, a Java script API is used.
THe result Obj returned from API contains data for each url.
The data is then sent back to as an AJAX Call for each url to a php file .
Can this be implemented Via CRON JOBS ?
OR
Is there any method to schedule javascript to run periodically, like cron for php?
UPDATE: i could manage the javascript call to API with PHP curl ,And the cron Job is getting executed perfectly. But i dont think it is the correct solution to this question may be Node.Js is the solution(i didnt test it yet).
You can't run Javascript in Cronjobs because Javascript is ran by browsers. I think you should take a look at curl in php to call an api instead.
http://www.php.net/manual/en/book.curl.php
You have to split the work: Cron the JS, Cron the PHP. In the middle, deliver one's results to another. Agree with phantomjs usage for JS execution (or casperJS-I prefer). Execute the JS, output to JSON as a file, read from the file using file_get_contents from PHP. And define these actions in two different cron jobs.
You can run Javascript via cron in a Javascript runtime like node.js: http://nodejs.org/
phantomjs is one possibility, see this thread wget + JavaScript?
Otherwise you could run Node.js on your server to execute JavaScript in a CLI type environment but mixing node.js and PHP could become complicated.
you can schedule javascript with cron by using Node.js

Create Callback from Python

I have a python script that gets called from some javascript code, that I have running. And when the python script is finished I want a call back to go to a Java program of mine, I was trying to make a html page and then check with that, but I am running the Java program locally and can't connect to the FTP?
How could this be accomplished?
Thanks
EDIT
Here is how the flow works
Java calls a javascript function in browser on my local machine ->
Javascript calls python with POST Request on my Server ->
I want a callback to Java to know when Python is done ->
So that Java can move on
Do you see know how it works?
If you are running from the command line you could use pipes | which will call the next command in line with the standard out of the first command. As follows:
firstCommand | secondCommand
So if your Python script writes to standard out you could invoke it from the command line, and have the Java execute from the command line also as the second command receiving it's input from standard in.

How to unpack Javascript in Python

I would like to retrieve the contents of a javascript script instead of executing it upon requesting it.
EDIT: I understand that Python is not executing the javascript code. The issue is that when I request this online JS script it gets executed. I'm unable to retrieve the contents of the script. Maybe what I want is to decode the script like so http://jsunpack.jeek.org/dec/go
That's what my code looks like to request the js file:
def request(self, uri):
data = None
req = urllib2.Request(uri, data, self.header)
response = urllib2.urlopen(req)
html_text = response.read()
return html_text.decode()
I know approximately what the insides of the script look like but all I get after the request is issued is a 'loaded' message. My guess is that the JS code gets executed. Is there any way to just request the code?
There is no HTML or JavaScript interpreter in urllib2. This module does nothing but fetch the resource and return it to you raw; it certainly will not attempt to execute any JavaScript code it receives. If you are not receiving the response you expect, check the URL with a tool like wget or monitor the network connection with Wireshark or Fiddler to see what the server is actually returning.
(decode() here only converts the bytes of the HTTP response body to Unicode characters—using the default character encoding, which probably isn't a good idea.)
ETA:
I guess what I want is to decode the Javascript like so jsunpack.jeek.org/dec/go
Ah, well that's a different game entirely. You can get the source for that here, though you'll also need to install SpiderMonkey, the JavaScript engine from Mozilla, to allow it to run the downloaded JavaScript.
There's no way to automatically ‘unpack’ obfuscated JavaScript without running it, since the packing code can do anything at all and JS is a Turing-complete language. All this tool does is run it with some wrapper code for functions like eval which packers/obfuscators typically use. Unfortunately, this sabotage is easily detectable, so if it's malware you're trying to unpack you'll find this fails as often as it succeeds.
I'm not sure I understand. If I do a simplified version of your code and run it on a URI that's sure to have some javascript:
>>> import urllib2
>>> res = urllib2.urlopen("http://stackoverflow.com/questions/6946867/how-to-unpack-javascript-in-python")
And you print res (or res.decode()), the javascript is intact.
Doing urlopen should retrieve whatever character stream the source provides. It's up to you to do something with it (render it as html, interpret it as javascript, etc).

Categories