OK, so some more details. This is a multisite set up where I have set the privacy option so that it is only visible to logged in users for that particular site. Though changing it to public still generates an error.
I have experimented with switching out the file_get_contents() with CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link . (strpos($link, '?') === false ? '?' : '&') . 'pdf-template');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
$html = $file;
but run in to the same issues of the content not loading and now I am experimenting with wp_remote_retrieve_body()
Effectively changing
$html = file_get_contents($link . (strpos($link, '?') === false ? '?' : '&') . 'pdf-template', false, $context);
to
$getargs = array(
'cookies' => $_COOKIE,
);
$html = wp_remote_retrieve_body( wp_remote_get( $link . (strpos($link, '?') === false ? '?' : '&') . 'pdf-template', $getargs ) );
Yet it only works if I set the privacy option to publically visible.
This is driving me nuts.
Hi, Neocreo!
Have you tried it with simply define('FETCH_COOKIES_ENABLED', true); defined in your wp-config.php fle?
Cheers!
I have that defined already, but it still has problems loading the stream.
Hmmmm, maybe the request for the page is done before the cookies are completely set… I shall experiment.
Hmmm, four weeks on and still no luck. However, I was just sitting and trying to get it to work when the following happened:
I wrote “/pdf-template/” instead of “/pdf-preview/”, and suddenly the page loaded.
I had before this reset the $html variable to fetch the contents the old fashioned way.
The preview and the pdf links still won’t work, but maybe that can help you point me in the right direction?
OK,So I finally cracked it!
I just could not get the file_get_contents() to work.
So I have experimented with some other ways of retrieving the content as evidenced by my other posts.
The solution was actually using wp_remote_get() in combination with wp_remote_retrieve_body().
And bypassing the PHPSESSID cookie to allow me to get the contents behind this membership-plugin I use (all users have to be logged in to view content).
I hope this helps anyone encountering this issue, and I would like to suggest to move to the built in wp_remote functions rather than file_get_contents in future releases.
Full solution:
$fullink = $link . (strpos($link, '?') === false ? '?' : '&') . 'pdf-template' ;
$cookies = array();
foreach ( $_COOKIE as $name => $value ) {
if ( 'PHPSESSID' === $name )
continue;
$cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
}
$filegetargs = array(
'cookies' => $cookies,
'sslverify' => false,
);
$response = wp_remote_get( $fullink, $filegetargs );
if(is_wp_error($response)){
echo 'Error Found ( '.$response->get_error_message().' )';
}
if( is_array($response) ) {
$html = wp_remote_retrieve_body( $response );
}