views:

762

answers:

4

Hello, i want to provide most possible flexibility for my script and so i need all possible ways in php and javascript to read the content(not sourcecode) of a php file from a remote server. so far i found curl, fopen and include for php and none for javascript, but i dont even know if this is possible with javascript. Thanks for any hint.

A: 

Javascript is a client side scripting language primarily, you can't just simply get an external resource with it without either

  • server-side help ( xhr to server-side page to do curl/wget )
  • the resource has to be on your domain and you can XMLHttpRequest it without server-side help.
meder
+1  A: 

You've got the major options for PHP figured out.

As for javascript (assuming it's running in a web browser), the same-origin policy will complicate things.

Possible workaround for Javascript include:

  • Using a script-tag proxy

  • Using a PHP proxy script on the domain that your page is loaded from. Your javascript asks the PHP script to grab the remote content. PHP script does that, and outputs the contents back to you javascript.

timdev
A: 

ok, i need to check wget and those proxy things. thanks your posts, you helped me pretty much. if anyone else has another possible solution in mind please share it. is it possible to do it via popen and cmd or somehow else? wordpress for example is still able to recieve information from the original website on a server with include and fopen off and no curl included. how can they do that? its showing me outdated plugins for example. i'm not that familar with wordpress and their file structure so i didnt found the relevant part in the files.

Giovanni
if you use a free or cheap shared server probably you can't use popen, shell commands or curl. Create a PHP file with this content: <?php php_info(); ?> then show us the result and perhaps we can help you.
Pedro Ladaria
+1  A: 

PHP:

  - fopen() + fread()
  - file_get_contents()
  - curl
  - executing shell commands
        `wget 'www.google.com' -O saved.htm`;
        $result = `cat saved.htm`;

JavaScript:

  // not for remote server
  var req = new XMLHttpRequest();
  req.open('GET', 'http://www.google.com', false);
  req.send(null);
  if (req.readyState==4) alert(req.responseText);
Pedro Ladaria