• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

A javascript which will 'pull' info from a remote server...

Mr. Goodie2Shoes

Well-known member
okay, so I am creating a file repository kinda script for my forum Note: The thing I am asking is not related to XenForo, it's a totally stand-alone script...

and I'll be using AJAX to pull the directories and files from my remote server and will be shown to my visitors. There will be a PHP file in the remote server which will do everything and will send the info to the Javascript, I've set everything up and is working perfectly! but the problem is that the JavaScript and the PHP file have to be in the same server (the remote one)...

if there any way to keep the JavaScript in the main server and still get it to work... I think its possible via cURL, but this function really confuses me so any help is appreciated :)


XenForo Developer
Staff member
You probably need to create a PHP proxy on your main server and have your JS/AJAX script talk to that, instructing it to go fetch the requested data from your remote server via cURL or something similar.


When the remote script is loaded, perhaps have it output to an .xml format. Which your client-side javascript can parse and spit out in the browser, even with ajax.
$files = scandir('/full/path');
foreach($files as $key => $value)
// do something with $value;
// such as an XML type output
or ..

class CreateFeed
	var $channel_url; 
	var $channel_title; 
	var $items = array(); 
	var $nritems; 

	function CreateFeed()
	function SetChannel($url, $title, $description, $lang, $copyright, $creator, $subject)
	function SetItem($url, $title, $description)
	function Output()
	$output = '<?xml version="1.0" encoding="UTF-8"?>'."\n"; 
	$output .= '<dirlist version="0" xmlns = "http://xspf.org/ns/0/">'."\n"; 
	$output .= '<all>'."\n"; 
	for($k=0; $k<$this->nritems; $k++)
		 $output .= '<file>'."\n"; 
		 $output .= '<location>'.$this->items[$k]['url'].'</location>'."\n"; 
		 $output .= '<image></image>'."\n"; 
		 $output .= '<annotation>'.$this->items[$k]['title'].'</annotation>'."\n"; 
		 $output .= '</file>'."\n"; 
	$output .= '</all>'."\n"; 
	$output .= '</dirlist>'."\n"; 
	return $output; 
} // class

$myfeed = new CreateFeed(); 
$dir = opendir ("/where/should/we/be/scanning/"); 
while (false !== ($file = readdir($dir)))
	if (strpos($file, '.txt',1)||strpos($file, '.doc',1) ) 
		$myfeed->SetItem("http://remotefeed.example.com/dir/$file", "$file", ""); 
echo $myfeed->output(); // or write to file.


By the way, regarding the "using jQuery to parse remote XML" part of things, what Kier means with "use a php proxy" he's referring to this: http://en.wikipedia.org/wiki/Same_origin_policy

Something like this: local server testxml/index.php
$remotefile = 'http://mrfloris.com/testxml/index.php';
$proxy = curl_init($remotefile);
curl_setopt($proxy, CURLOPT_RETURNTRANSFER, true);
curl_setopt($proxy, CURLOPT_HEADER, false);
$data = curl_exec($proxy);
if ($data === false)
  echo 'cURL failed';
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
var xml = <?php echo $data; ?>;
var name = $(this).find("title").text();
var email = $(this).find("location").text();
document.write("<b>Title</b>: "+title+"<br>");
document.write("<b>Location</b>: "+location+"<br>");


Above post doesn't seem to work, I am not that great with javascript.
It works, the var xml gets populated, I just am not sure how to do "for each $this-> whatever, echo key:value"
Maybe Kier knows what I do wrong.

output of above script
Screen Shot 2012-05-01 at 1.14.21 PM.png