0

I'm sorry if I'm missing something obvious like having " instead of ' but I tried many different ways and it still does the same thing, i.e. displays the actual javascript code, instead of the functionality, when I put it inside a .html file or gives me this error: Parse error: syntax error, unexpected '<' in E:\XAMPP\htdocs\website2\test.php on line 9 When I place it inside of a php file.

Here's the code:

<!DOCTYPE html>
<html>
<head>
    <script src="js.js" type="text/javascript"></script>
    <title>TEST</title>
</head>
<body>
<?php 
    echo '<script type="text/javascript">
    var https = require("https");
var username = "04d2ac7f76a0fbc0eee9dc5ef96b9259";
var password = "dc70ffc7ad911236bc2e0822855e2d42";
var auth = "Basic " + new Buffer(username + ':' + password).toString('base64');
var request = https.request({
    method: "GET",
    host: "api.intrinio.com",
    path: "/companies?ticker=AAPL",
    headers: {
        "Authorization": auth
    }
}, function(response) {
    var json = "";
    response.on('data', function (chunk) {
        json += chunk;
    });
    response.on('end', function() {
        var company = JSON.parse(json);
        console.log(company);
    });
});</script>'; 
?>
</body>
</html>
Frenzyy
  • 3
  • 3
  • 4
    Why are you echoing out an entire script (with syntax errors, see your single quotes on the response.on method listeners) when you could just scrap the php tag and put them in normally? – Sterling Archer Dec 17 '17 at 17:00
  • 1
    Its not working because you need to fully escape all single quotes if you are going to `echo` it out in single quotes. But as @SterlingArcher said... theres nothing php dynamic in that output, so why bother echoing it with php? – IncredibleHat Dec 17 '17 at 17:01
  • I thought that's the correct way to do it... Still not good at this- sorry. Having said that, when I just run the script on its own, without the php tags, it still just displays the actual code, not the functionality which I expect from it. This is what it should display: https://intrinio.com/tutorial/web_api – Frenzyy Dec 17 '17 at 17:03
  • 1
    Sounds like you haven't set up PHP properly and it's not rendering – Sterling Archer Dec 17 '17 at 17:05
  • It displays the code? Where? In the web browser window, you literally see every character of the javascript code in the blank white page? I'm confused now lol! – IncredibleHat Dec 17 '17 at 17:06

4 Answers4

3

Why just not do without the php tag? The error is because you not escape the single quotes in your code.

Example without php:

<!DOCTYPE html>
<html>
<head>
    <script src="js.js" type="text/javascript"></script>
    <title>TEST</title>
</head>
<body>
<script type="text/javascript">
    var https = require("https");
var username = "04d2ac7f76a0fbc0eee9dc5ef96b9259";
var password = "dc70ffc7ad911236bc2e0822855e2d42";
var auth = "Basic " + new Buffer(username + ':' + password).toString('base64');
var request = https.request({
    method: "GET",
    host: "api.intrinio.com",
    path: "/companies?ticker=AAPL",
    headers: {
        "Authorization": auth
    }
}, function(response) {
    var json = "";
    response.on('data', function (chunk) {
        json += chunk;
    });
    response.on('end', function() {
        var company = JSON.parse(json);
        console.log(company);
    });
});</script>
</body>
</html>

Example with php:

<!DOCTYPE html>
<html>
<head>
    <script src="js.js" type="text/javascript"></script>
    <title>TEST</title>
</head>
<body>
<?php 
    echo '<script type="text/javascript">
    var https = require("https");
var username = "04d2ac7f76a0fbc0eee9dc5ef96b9259";
var password = "dc70ffc7ad911236bc2e0822855e2d42";
var auth = "Basic " + new Buffer(username + ':' + password).toString(\'base64\');
var request = https.request({
    method: "GET",
    host: "api.intrinio.com",
    path: "/companies?ticker=AAPL",
    headers: {
        "Authorization": auth
    }
}, function(response) {
    var json = "";
    response.on(\'data\', function (chunk) {
        json += chunk;
    });
    response.on(\'end\', function() {
        var company = JSON.parse(json);
        console.log(company);
    });
});</script>'; 
?>
</body>
</html>

But more important this is a node.js code and not a client-side javascript. I recommend you get some node and javascript tutorials.

Ciro Spaciari
  • 630
  • 5
  • 10
  • I'm sorry but am I doing something wrong if I'm getting a blank page when I use your code? For both of your answers... Thanks. – Frenzyy Dec 17 '17 at 17:09
  • 1
    The script tag is not visible, you need to use Developer Tools (F12 in Chrome) to see the scripts and the console.log output. – Ciro Spaciari Dec 17 '17 at 17:13
  • So how would I go about displaying the output which I can otherwise get from this link? https://api.intrinio.com/companies?ticker=AAPL Sorry if it's a stupid question but I'm struggling about with JS as you can see... – Frenzyy Dec 17 '17 at 17:22
  • This script also appears to be node.js and not client side script, I suggest you get some [node](https://www.youtube.com/watch?v=w-7RQ46RgxU&list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp) and [javascript](https://www.youtube.com/watch?v=XL9Ri8pO68w) tutorials. – Ciro Spaciari Dec 17 '17 at 17:26
0

You should escape ' in with \' in your code.

Ivan Jelev
  • 115
  • 3
0

You're right in suspecting you've placed ' and " in improper places without escaping, as pointed out by Ivan.

<!DOCTYPE html>
<html>
<head>
  <script src="js.js" type="text/javascript"></script>
  <title>TEST</title>
</head>
<body>
<?php 
  echo '<script type="text/javascript">
  var https = require("https");
  var username = "04d2ac7f76a0fbc0eee9dc5ef96b9259";
  var password = "dc70ffc7ad911236bc2e0822855e2d42";
  var auth = "Basic " + new Buffer(username + \':\' + password).toString(\'base64\');
  var request = https.request({
    method: "GET",
    host: "api.intrinio.com",
    path: "/companies?ticker=AAPL",
    headers: {
      "Authorization": auth
    }
  }, function(response) {
    var json = "";
    response.on(\'data\', function (chunk) {
      json += chunk;
    });
    response.on(\'end\', function() {
      var company = JSON.parse(json);
      console.log(company);
    });
  });</script>';
?>
</body>
0

Echo function is used to output the string and you are echoing a code statement which is not a valid string. Put it as in single line as:

<?php echo '<script type="text/javascript">var https = require("https");var username = "04d2ac7f76a0fbc0eee9dc5ef96b9259";var password = "dc70ffc7ad911236bc2e0822855e2d42";var auth = "Basic " + new Buffer(username + ':' + password).toString(\'base64\');var request = https.request({method: "GET",host: "api.intrinio.com",path: "/companies?ticker=AAPL",headers: {"Authorization": auth}}, function(response) {var json = "";response.on(\'data\', function (chunk) {json += chunk;});response.on(\'end\', function() {var company = JSON.parse(json);console.log(company);});});</script>';?>
Raazi
  • 1