1. 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...

Discussion in 'General PHP and MySQL Discussions' started by Mr. Goodie2Shoes, May 1, 2012.

  1. Mr. Goodie2Shoes

    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 :)
  2. Kier

    Kier 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.
    Brentnauer likes this.
  3. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    yes, but I don't know how to do that... :p
  4. Floris

    Floris Guest

    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');
    $files as $key => $value)
    // do something with $value;
    // such as an XML type output
    or ..

    class CreateFeed

    $items = array(); 

    $output '<?xml version="1.0" encoding="UTF-8"?>'."\n"
    $output .= '<dirlist version="0" xmlns = "http://xspf.org/ns/0/">'."\n"
    $output .= '<all>'."\n"
    $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"
    // 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->output(); // or write to file.
  5. Floris

    Floris Guest

    parsing xml with javascript; http://www.compoc.com/tuts/

    var xml = "<dirlist><all><file>Beethoven</file></all></dirlist>";
    var result = $(xml).find("file").text();
  6. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    Thanks Floris, I'll try both PHP proxy and the xml system and see which one works...
  7. Floris

    Floris Guest

    The code I gave you is meant as example, it's not your 100% solution.
  8. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    Yeah I know that... but the structure is the same, right? :)
  9. Floris

    Floris Guest

    It seems I was almost right; http://mrfloris.com/testxml/
    (right click to see the XML output for .txt files (should be 2 of them).

    Screen Shot 2012-05-01 at 12.37.42 PM.png

    Screen Shot 2012-05-01 at 12.37.52 PM.png

    The next step is to remote trigger the php and read the output and parse it
    so you can for example say <a href="{location}">{title}</a>

    Attached Files:

  10. Floris

    Floris Guest

    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
    $proxy curl_init($remotefile);
    $data curl_exec($proxy);
    if (
    $data === false)
    '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>");
    Mr. Goodie2Shoes likes this.
  11. Floris

    Floris Guest

    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

Share This Page