0

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>
Marcus Silverman
  • 243
  • 1
  • 7
  • 22
  • Based on what i see it's just a post API why do you want to display this javascript and not just make the API call on php using guzzle , curl etc etc and then handle the response. – pr1nc3 Oct 30 '18 at 12:41
  • `require('request')` is node.js code you can't have node.js inside php file in script tag you need to use webpack to convert require, or use node but not in script tag. – jcubic Oct 30 '18 at 12:54
  • Ok, thanks so much. But do I need node.js or is it possible to do a request similar to what I have written above? Thanks! – Marcus Silverman Oct 30 '18 at 12:55
  • You can use ajax wiith XMLHTTPRequst object or new fetch API but only if that site have CORS enabled (special http headers) if not then you will need server proxy in php. You can use curl to fetch the page (in php) and use ajax to communicate between javascript code and php. – jcubic Oct 30 '18 at 12:58
  • Here is question [How to make a PHP proxy to solve CORS error?](https://stackoverflow.com/questions/26576641/how-to-make-a-php-proxy-to-solve-cors-error) Instead of jQuery `$.ajax` you can use [fetch function](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) – jcubic Oct 30 '18 at 13:01
  • With php proxy you will also be able to hide `api_key` from users to see. – jcubic Oct 30 '18 at 13:04
  • Thanks so much. Curl is actually an option.. but how would I implement this?? – Marcus Silverman Oct 30 '18 at 13:52
  • curl -H "Content-Type:application/json" -X POST -d '{"start_date":"2016-08-02","end_date":"2016-08-03", "columns":["paid_impressions","revenue"]}' 'http://udmserve.com/udm/radalytics_api.cpx?action=report&api_key=xxxx' – Marcus Silverman Oct 30 '18 at 13:52

2 Answers2

1

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"]
]]);
0

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.

  • This will not work because `require('request')` is node.js code that can't be put in script tag (it will only work with webpack in external file but request is node.js library is event with webpack it will not work). – jcubic Oct 30 '18 at 12:56