1

I make a curl Request to get some Javascript Code back. When i execute the php in terminal, i get the Javascript code. But when i try it with a browser the javascript code is not shown.

I tried to echo it with json_encode. But i only get a false back. I also tried here doc. but nothing seems to work.

Maybe because the javascript code i get back starts like this:

 !function(n,t,d){function e(n,t){

How can i echo this javascript code for browser. In terminal it works fine.

<?php
        function getUrlCurl($url) {
            $curl_handle = curl_init();
            curl_setopt ($curl_handle, CURLOPT_URL, $url);
            curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1);
            $buffer = curl_exec($curl_handle);
            $info = curl_getinfo($curl_handle);
            curl_close($curl_handle);
            return $buffer;
    }


$response = getUrlCurl(myUrl);
?>
<html>
<head>
<script>
<?php echo json_encode($response); ?>
</script>
<body>
</body>
</html>
ah75
  • 21
  • 2
  • 2
    Could you please share some PHP code related to curl requesting and printing the code? A minimal case would help others to reproduce the problem. – FelisCatus Sep 07 '15 at 10:39
  • 1
    What HTML do you get when you run the PHP. Look at the source code in your browser. What do you expect to get? Why aren't you just using ``? – Quentin Sep 07 '15 at 10:48
  • the company that wants me to put their code in our site has some restrictions. i cannot put the code in an script src. they want me to echo it in an script tag – ah75 Sep 07 '15 at 10:49
  • what i expect to get is: – ah75 Sep 07 '15 at 10:51
  • If you only see `false` as the result, then the curl execution has failed. Please follow the answer in http://stackoverflow.com/questions/3757071/php-debugging-curl and get more verbose log. Then you can add more information in the original question. – FelisCatus Sep 07 '15 at 11:01
  • And also fopen is not allowed on our servers. So that's the reason i have to make a curl request. so, no file_get_contents allowed. – ah75 Sep 07 '15 at 11:02
  • no, the curl execution has not failed. as i said, it works when i execute the php file in terminal. i get the response i expect in terminal. – ah75 Sep 07 '15 at 11:04
  • But it can fail in browser even if terminal works. It can be due to different configurations or environments. When in doubt, check the log to trace the request. – FelisCatus Sep 07 '15 at 11:06
  • but i only get false, when i use json_encode. – ah75 Sep 07 '15 at 11:27
  • without i get nothing back – ah75 Sep 07 '15 at 11:27
  • 1
    @FelisCatus: you were right. it was the proxy server. thank you :) – ah75 Sep 07 '15 at 11:53
  • i had to give a proxy in the curl function – ah75 Sep 07 '15 at 11:54
  • @ah75 Could you please share your knowledge with others by kindly adding an answer with code about the proxy settings? You can mark it as accepted. – FelisCatus Sep 07 '15 at 12:02

1 Answers1

0

This may be over simplifying things, but you could try something like this;

<?php
    $response = file_get_contents(myUrl);
?>
<html>
    <head>
        <script>
            <?php echo $response; ?>
        </script>
    </head>
<body>
</body>
</html>

Also, it may be worth mentioning, that in your code sample, there is no closing <head> tag. That could also be causing you to have issues.

guyver4mk
  • 609
  • 6
  • 11