XF 2.0 Callback to include an external file

Dragonsys

Member
I'm trying to include an external file into a help page, but it is just coming out blank.
I have my callback file in /src/addons/DSO/Privacy.php
PHP:
<?php

namespace DSO;
class Privacy {
  public static function getHtml(){
    $url = 'https://www.domain.com/myfile.txt';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
  }
}

My page has this for the content:
HTML:
<xf:callback class="DSO\\Privacy" method="getHtml"></xf:callback>

I don't get any errors, but the page content is blank. I have verified the remote file exists and is readable. Curl is installed and working on both servers, I'm not sure what I might be missing.
Page URL - https://www.dragons-server.com/help/privacy/

1521613346032.webp
 
Where the callback value be used?
HTML:
<xf:set var="$privacyHtml"><xf:callback class=""
                                        method=""></xf:callback></xf:set>
{$privacyHtml|raw}
 
I'm trying to use it on a Help page. Do I need to add an extend Help?
I changed the Page content to the below, but I still don't get output on the page.
HTML:
<xf:set var="$privacyHtml">
    <xf:callback class="DSO\\Privacy" method="getHtml"></xf:callback>
</xf:set>
{$privacyHtml|raw}

every post I have found about callbacks don't mention needing anything in the template other than the xf:callback statement.
 
Ok, I did a dump and I get the below. So now I need to figure out if the issue is in the class php or in the file I am trying to get with curl.

PHP:
PreEscaped {#238 ▼
  +value: """
    \n
    \t\n
    """
  +escapeType: "html"
}
 
If you're using a callback to include a page, the callback must return the rendered page. You can't set a variable to equal a callback. Well, you can but 9 times out of 10 it won't act like you expect.

https://xenforo.com/community/threads/php-callback-inside-if.142488/#post-1221123

If your cURL call is returning the contents of the file (not the file itself), remove the <xf:set var="$privacyHtml"> portions of your code and the {$privacyHtml|raw} and it should display directly from the callback.

If your cURL call is returning the file itself, you need to do the above, but parse the contents of the file into a variable and return the variable.
 
Last edited:
It should be returning the file contents. I originally had it set, as I showed in the OP, but I still get a bank page. I will try adding a parse and see if it is returning the file instead of the contents.
 
I shouldn't have deleted my original reply, it does work.

I tested your code on my development server and it works fine with just the callback. This is with the callback inserted into the page container body before the first DIV.

text.webp

All you need is this in your template...
Code:
<xf:callback class="DSO\Privacy" method="getHtml"></xf:callback>

But the text does need to be HTML formatted.
 
This may be a dumb question...

But what style/template did you add that callback to?

Could you have added it to the wrong style? Or, a style/template that isn't common to the one you are linking to?
 
This may be a dumb question...

But what style/template did you add that callback to?

Could you have added it to the wrong style? Or, a style/template that isn't common to the one you are linking to?
I added it to a help page, so it should be the same in every style.
as a test I did try adding it to the page_container on my main template, still nothing. So I think it must be something with my server itself.
 
Back
Top Bottom