3 Replies - 21618 Views - Last Post: 05 May 2011 - 01:16 PM

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Pulling data from external web page

Posted 05 May 2011 - 10:17 AM

Hey all, I'd like to pull data from an external web server. I intend to put the below html/js on webserver A, and when the page is visited, it will pull data from webserver B (google.com)

Why isn't it working?


<html>

<!--                             HTML REDIRECT/ SECRET GRAB
	/* This is my html redirect script.  The way it works is you put it on the server, and you
	point it at another server.  The script will then load data from the server it's pointing to.  
	This method allows other people to be able to have write access to content on the webserver, 
	with out actually having write access to the web server.  
	
-->

<head>

<script>
function showData()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    alert('hello');  // I never see this alert...
	    document.getElementById("theBody").innerHTML=xmlhttp.responseText;
	    }
	  }
	xmlhttp.open("GET","http://www.google.com");
	xmlhttp.send();
	alert('http req sent...');
}
</script>

</head>

<body onload="showData();" id="theBody">
</body>


</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Pulling data from external web page

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Pulling data from external web page

Posted 05 May 2011 - 10:25 AM

the AJAX policy (SOP - Same Origin Policy) does not allow to connect to a different server.

you can circumvent that by connecting to your own server and do the google request from there (e.g. by cURL)
Was This Post Helpful? 1
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Pulling data from external web page

Posted 05 May 2011 - 11:22 AM

Cool, thanks for the tip Dormilich. Here's what my php file looks like now:

(getpageExample.php)
<?php

/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */
function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

$dalas = get_web_page("http://www.google.com");
echo $dalas['content'];


?>



Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Pulling data from external web page

Posted 05 May 2011 - 01:16 PM

This will work just fine but are you sure you want to hard-code the URL?

You have the option of sending it via GET or POST with AJAX.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1