• 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
#1
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 :)
 

Kier

XenForo Developer
Staff member
#2
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.
 
F

Floris

Guest
#4
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.
PHP:
$files = scandir('/full/path');
foreach($files as $key => $value)
{
// do something with $value;
// such as an XML type output
}
or ..

PHP:
<?php
class CreateFeed
{ 
	var $channel_url; 
	var $channel_title; 
	var $items = array(); 
	var $nritems; 

	function CreateFeed()
	{ 
		$this->nritems=0; 
		$this->channel_url=''; 
		$this->channel_title=''; 
	} 
	function SetChannel($url, $title, $description, $lang, $copyright, $creator, $subject)
	{ 
		$this->channel_url=$url; 
		$this->channel_title=$title; 
	} 
	function SetItem($url, $title, $description)
	{ 
		$this->items[$this->nritems]['url']=$url; 
		$this->items[$this->nritems]['title']=$title; 
		$this->nritems++; 
	} 
	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.
 
F

Floris

Guest
#10
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
PHP:
<?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);
curl_close($proxy);
 
if ($data === false)
{
  echo 'cURL failed';
  exit;
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>
var xml = <?php echo $data; ?>;
 
$(xml).find("file").each(function(){
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>");
})
 
</script>
 
F

Floris

Guest
#11
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