简体   繁体   中英

Using curl or file_get_contents in Joomla administrator component

I have built an administrator component in Joomla with several views. One view 'recap' uses AJAX to call out to view 'details' in format 'raw' and inserts the HTML into the page (works great). I now have a 'pdf' format for view 'recap' that I would like to fill with the same contents I get from view 'details' in format 'raw'. I have a successful "Hello World" of the PDF, but unable to determine how to use PHP to get the 'details' view and fill in the PDF.

I have tried the following:

$this->loadTemplate('details');

Which fails because I can't specify format 'raw', Joomla error: Layout default_details not found.

file_get_contents('same url as successful AJAX call');

Which fails with error log: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden. Using the same strategy with curl fails on authentication as well. I know I can pass authentication details with the curl call, but it appears that I can only get the username and hashed password of the current user. I suppose I could hardcode in credentials for a user that does have access but that seems insecure.

At this point, I can only think to replicate all the code in detail's default.php and utilize its model to get the data, but the good programmer in me is screaming "NO!!!".

As I understand your question, you want to serve both an HTML and a PDF encoding for a page. The HTML view is using client-side AJAX to load data from some other view and display it on the page. This doesn't work for the PDF view, because the PDF file needs to be rendered server-side.

The file_get_contents() approach doesn't make sense, you are trying to render a PDF file to serve from dynamic content (not a static file saved in your filesystem).

You can make the call to get the other view to include in your PHP code, rather than doing it client-side. The approach to doing so depends on whether the other view is a component or module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM