Hi,

I cannot load a URL into a DIV tag using javascript and ajax, all that occurs is the original div text remains in place:

<html>
  <head>
    <title>Ajax Example</title>

  </head>
  <body>
      <center />
      <h1>Loading a web page into a DIV</h1>
      <div id='info'>This sentence will be replaced</div>
      <script>

          params = "url=google.com"
          request = new ajaxRequest()
          request.open("POST", "urlpost.php", true)
          request.setRequestHeader("Content-type",
                "application/x-www-form-urlencoded")
          request.setRequestHeader("Content-length", params.length)
          request.setRequestHeader("Connection", "close")

          request.onreadystatechange = function()
          {
              if (this.readyState == 4)
              {
                  if (this.status == 200)
                  {
                      if (this.responseText != null)
                      {
                          document.getElementById('info').innerHTML =
                              this.responseText
                      }
                      else alert("Ajax error: No data received")
                  }
                  else alert("Ajax error: " + this.statusText)
              }
          }

          request.send(params)

          function ajaxRequest()
          {
              try
              {
                  var request = new XMLHttpRequest()
              }
              catch(e1)
              {
                  try
                  {
                      request = new ActiveXObject("Msxml2.XMLHTTP")
                  }
                    catch(e2)
                    {
                        try
                        {
                            request = new ActiveXObject("Microsoft.XMLHTTP")
                        }
                        catch(e3)
                        {
                            request = false
                        }
                    }
              }
              return request
          }
      </script>
  </body>
</html>

I use a php script to make the server side call:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php // urlpost.php
        if (isset($_POST['url'])) {
            echo file_get_contents("http://".SanitizeString($_POST['url']));
        }

        function SanitizeString($var) {
            $var = strip_tags($var);
            $var = htmlentities($var);
            return stripslashes($var);
        }
        ?>
    </body>
</html>

Can anyone I assist?
I ensured the proxy details are correct to make an external connection also.

Thanks.

You Javascript syntax is wrong. For one, most semi-colons are missing.

I am using an IDE to code javascript.

You don't need to use semi colons unless there is more than one statement on a line.

The syntax is correct, the issue fails when at this error:

else alert("Ajax error: " + this.statusText)else alert("Ajax error: " + this.statusText)

The URL either cannot be retrieved or displayed since it fails to correctly replace the DIV text:

<div id='info'>This sentence will be replaced</div>

Other than the syntax, I need to debug why the URL does not replace the DIV text.

If you are using an IDE, can't you just set breakpoints and see what happens? Are you sure that file_get_contents is allowed to open an external URL?

You don't need to use semi colons

Although correct, there are many reasons to use them consistently.

I am debugging the file now.

How do I allow:

if (isset($_POST['url'])) {
            echo file_get_contents("http://".SanitizeString($_POST['url']));
        }

"file get contents" access to an external URL?

I am using Netbeans but not sure where the option is in the menu to allow access to an external URL.

http://php.net/file_get_contents

Read the tip about the fopen wrappers, it's in the PHP.INI. You may be able to change this if you are running locally. If you are running on a shared web host, this is a feature they will NOT enable. In that case I suggest you use cUrl instead.

It is turned on for fopen, am running it locally:

    Directive   Local Value Master Value
allow_url_fopen On          On

Any other suggestions to get it running locally?

I am sure that google.com does a redirect. I am not sure that the file_get_contents function follows it. Try it with a specific text file.

It works for opening a text file locally in the php file:

echo file_get_contents("/home/user/".($_POST['url']))

and in the javascript file:

params = "url=text.txt"

Then to test opening a remote url I did the following:

I added a user agent call in the php file but no luck

ini_set( Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0);
if (isset($_POST['url'])) {
    echo file_get_contents("http://".SanitizeString($_POST['url']));
    }

Any further ideas to display a remote url?

Use cUrl.

I am trying to integrate it into the php script so that it can read the "params = url" in the javascript file but it doesn't work:

echo "<pre>";
       if (isset($_POST['url'])) {
           $ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'url'); //replaced 'http://example.com' with 'url'
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

// display file
echo $file_contents("url".SanitizeString($_POST['url']));
        } //replaced "http://" with "url"

        function SanitizeString($var) {
            $var = strip_tags($var);
            $var = htmlentities($var);
            return stripslashes($var);
        }

It doesn't work replacing the specific "http://example.com" with "url" , so as to reference it to the params in the javascript file.

params = "url=w3schools.com"

How can I correctly reference the params = "url=example.com" in the JS file with the curl function in the PHP file?

Line 5:

curl_setopt($ch, CURLOPT_URL, $_POST['url']);

Unfortunately it did not work either:

<?php // urlpost.php
        ini_set('user_agent', 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0)
            Gecko/20100101 Firefox/11.0');
        echo "<pre>";

        if (isset($_POST['url'])) {
            $ch = curl_init();
            $timeout = 5; // set to zero for no timeout
            curl_setopt ($ch, CURLOPT_URL, $_POST['url']);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $file_contents = curl_exec($ch);
            curl_close($ch);

// display file
        echo $file_contents("http://".SanitizeString($_POST['url']));
        }

        function SanitizeString($var) {
            $var = strip_tags($var);
            $var = htmlentities($var);
            return stripslashes($var);
        }
        echo "</pre>";
        ?>

Thanks for your help but I am running out of ideas about configuring "curl" and "file_get_contents" to work.

Could you possibly run the javascript with the php file to see if it works for you?
Remember to change the "params =url" in the JS file.

I have managed to get curl working by specifying a curl proxy statement in a separate PHP script, however I want to integrate it into the original "file_get_content" script:

if (isset($_POST['url'])) {
    $agent = $_SERVER["HTTP_USER_AGENT"];
        function file_get_contents_curl() {
    $ch = curl_init($_POST['url']);
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXY, "http://192.168.xx.xx:XXXX");
    curl_setopt($ch, CURLOPT_PROXYPORT, XXXX);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_get_contents_curl = curl_exec($ch);
    curl_close($ch);

    // display file
    echo $file_get_contents_curl("http://".SanitizeString($_POST['url']));
        }
    }
    function SanitizeString($var) {
            $var = strip_tags($var);
            $var = htmlentities($var);
            return stripslashes($var);
        }

Is the nesting of the functions correct/in the right order?
As it still does not load when I call with my javacript.

Is the nesting of the functions correct/in the right order?

No it's not. You call the function within the function.

Finally got it to work by correcting the functions syntax and changing the 'RETURN TRANSFER' to 0:

$agent = $_SERVER["HTTP_USER_AGENT"];
    $timeout = 5; // set to zero for no timeout

    if (isset($_POST['url'])) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $_POST['url']);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_PROXY, "http://192.168.xx.xx:XXXX");
    curl_setopt($ch, CURLOPT_PROXYPORT, XXXX);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents_curl = curl_exec($ch);
    curl_close($ch);

    echo $file_contents_curl("http://".SanitizeString($_POST['url']));
    }
    function SanitizeString($var) {
            $var = strip_tags($var);
            $var = htmlentities($var);
            return stripslashes($var);
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.