We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42156
    • 3 Posts
    Hi guys,
    I´m using MODX REVO and have the problem to get the following code to work.

    Heres my code-snippet:
    <?php
    $handle = fopen("http://www.domain.com/some-page.asp", "rb");
    $contents = '';
    while (!feof($handle)) {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
    	
    echo $contents;
    ?>


    This code works without any problem in a normal PHP-file on my webserver (outside of MODX!), but it doesn´t if I insert this code into a MODX-snippet (neither saved as a static file nor in database).

    The output?
    The modx-page, where i included the snippet via [[mysnippet] or [[!mysnippet]], is served as a totally blank page - the source-code is completly empty. No errors listed under "REPORT > ERROR LOG".
    I cleared the cache serveral times as well.


    When I try some other way, like "file_get_contents", there´s the same problem. In a single php-file (outside of MODX) it works, implemented into MODX is doesn´t.


    The whole problem is some kind of weird and too complicated to explain in all its details. The workaround I have to realise is to get the content of a parsed (ASP)file and just to output the content in my modx-page.

    Any ideas?
    Is there maybe some system variable in modx which blocks the fopen-command?
    • There are other options to fetching the content of an external file. cURL comes to mind. AJAX also would do the job.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 42156
        • 3 Posts
        the cURL options works (again) in an external (php) file, inside of modx the output is still blank (as described above).

        $url = 'http://www.domain.com/some-file.asp';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);
         
        echo $data;


        Am I the only one with such problems? Don´t understand why that code is not executed by modx.

        AJAX, ...that could be an option.
        • Have you tried returning your value rather than echo it? I believe this is the way it's supposed to be handled in Revo.

          <?php
          $handle = fopen("http://www.domain.com/some-page.asp", "rb");
          $contents = '';
          while (!feof($handle)) {
            $contents .= fread($handle, 8192);
          }
          fclose($handle);
               
          return $contents;
          ?>


            Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. BlogTwitterLinkedInGitHub
            • 42156
            • 3 Posts
            Quote from: smashingred at Nov 24, 2012, 03:50 PM
            Have you tried returning your value rather than echo it? I believe this is the way it's supposed to be handled in Revo.

            Yes, I tried it in this way too, with no success.

            But, finally, I got two solution to work with:
            1) use the AJAX way (like sottwell noted)

            2) to use the following code:

            ob_start();
            include("http://www.domain.com/some-file.asp");
            $yourfile = ob_get_clean();
            
            echo $yourfile;



            thx for help.