Search code examples
javascriptphpxmlhttprequest

XMLHttpRequest unexpectedly returns a communications error


I am new to PHP and am having difficulties with a piece of code. I am using an example, from Learning PHP, MySQL & JavaScript. The intent is to display a webpage, retrieved from the web using PHP, in a <div>>. Rather than displaying the google.com main webpage, I obtain "Communications error: status: 500 Internal Server Error". Can anyone advise me of what I am doing wrong.

The directory structure is:

amvetsfl292.org
  public_html
    PHP

The files contained in the PHP directory are:

Filename           Size   Description            Permission
async_request.js    478   JScript Script File    0644
test_php.html      1427   Firefox HTML Document  0644
url_post.php        408   PHP File               0644

I execute the test script from Firefox Developer with the address

amvetsfl292.org/PHP/test_php.html

I am not sure why the google home page is not displayed.

The source code files follow.

test_php.html:

<!DOCTYPE html>

<html lang="en">

  <head>

    <title>Asynchronous Communication Example</title>

  </head>

  <body style="text-align:center;">

    <h1>Loading a web page into a DIV</h1>
    <div id="info">This sentence will be replaced</div>

    <script src="async_request.js" ></script>

    <script>
      var params = "url=https://www.google.com";
      var request = new async_request ( );

      request.open ( "POST", "url_post.php", true );
      request.setRequestHeader ( 
                "Content-type",
                "application/x-www-form-urlencoded" );

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

      request.send ( params );

    </script>

  </body>

</html>

async_request.js

/* jsl:option explicit */

function async_request ( )
  {
  try 
    {
    var request = new XMLHttpRequest ( );
    }
  catch ( e1 )
    {
    try 
      {
      request = new ActiveXObject ( "Msxml2.XMLHTTP" );
      }
    catch ( e2 )
      {
      try 
        {
        request = new ActiveXObject ( "Microsoft.XMLHTTP" );
        }
      catch ( e3 )
        {
        return ( false );
        }
      }
    }

  return ( request );

  }

url_post.php

<?php // url_post.php

if ( isset ( $_POST [ "url" ] ) )
  {
  echo file_get_contents ( "https://" . 
                           sanitize_string ( $_POST [ "url" ] ) )
  }


function sanitize_string ( $var ) 
  {

  if ( get_magic_quotes_GPC ( ) )
    {
    $var = stripslashes ( $var );
    }
  $var = strip_tags ( $var );
  $var = htmlentities ( $var );
  return ( $var );
  }

?>

Solution

  • url_post.php line 6 is missing a ';'