Marcus Silverman
Marcus Silverman

Reputation: 243

How to display javascript in a php page

How would I display this info in a php page? I got this script from a API guide from my ad supplier.

Thanks!

   <html>
<head>
 <script type="text/javascript">

var request = require('request'); // npm package request
request.post(
'http://udmserve.com/udm/radalytics_api.cpx?action=report&api_key=xxx',
{ json: {"start_date":"2018-10-28","end_date":"2018-10-29", "columns":["paid_impressions","revenue"]} },
function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body)
    }
}
);
</script>
</head>

<body>
<h1>
  the rev is:
  <script type="text/javascript">
    document.write(request)
  </script>
</h1>
</body>
</html>

Upvotes: 0

Views: 115

Answers (2)

Mohammad Ali
Mohammad Ali

Reputation: 94

If you want to send POST request from PHP code, you can use Guzzle library.

First you should install Guzzle with Composer, Then you can use Guzzle easily and send your POST request.

$client = new GuzzleHttp\Client();
$response = $client->post('http://udmserve.com/udm/radalytics_api.cpx?action=report&api_key=xxxx', [ 'json' => [
    'start_date' => '2016-08-02',
    'end_date' => '2016-08-03',
    'columns' => ["paid_impressions", "revenue"]
]]);

Upvotes: 1

I dont really know what you mean with adding javascript in a php page. I think everyone knows that you can close the php with the ?> tag and then use html?

Try this;

<?php
//Here is your php code
?>
<!-- Here is html -->
<script>
var request = require('request'); // npm package request
request.post(
'http://udmserve.com/udm/radalytics_api.cpx?action=report&api_key=xxxx',
{ json: {"start_date":"2016-08-02","end_date":"2016-08-03", "columns":["paid_impressions","revenue"]} },
function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body)
    }
}
);
</script>

I hope this works out for you? Otherwise you could use curl in php; How do I send a POST request with PHP? or for a simple GET request, just use file_get_contents function.

Upvotes: 0

Related Questions