I have installed TXNewsletters but am experiencing a problem when I try to send a newsletter. I get the following error message:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /..my server..../assets/modules/txnewsletters/txnewsletters.inc.php on line 1479
Warning: file_get_contents(http://mydomain.com/newsletter-1.html) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /....my servers...../assets/modules/txnewsletters/txnewsletters.inc.php on line 1479
Googling, I have found out that this happens because my host has disabled the PHP option ’allow_url_fopen’. This would normally allow a programmer to open, include or otherwise use a remote file using a URL, rather than a local file path. However, the TXNewsletters code seems to want to a URL rather than local file path.
On the host’s help pages, they provide some example code using cURL to overcome this problem:
Alternative for file_get_contents()
Instead of:
[tt]<?php
$file_contents = file_get_contents(’
http://example.com/’);
// display file
echo $file_contents;
?>[/tt]
Use this:
[tt]
<?php
$ch = curl_init();
$timeout = 5;
// set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, ’
http://example.com’);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
// display file
echo $file_contents;
?>[/tt]
However, if you receive errors you can use this:
[tt]<?php
$site_url = ’
http://example.com’;
$ch = curl_init();
$timeout = 5;
// set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $site_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
ob_start();
curl_exec($ch);
curl_close($ch);
$file_contents = ob_get_contents();o
b_end_clean();
echo $file_contents;
?>[/tt]
The original code in the TXNewsletters module is:
[tt]function sendHTML ($newsletterId) {
global $modx;
ini_set(’default_socket_timeout’, 120);
$url = ’http://’.$_SERVER[’HTTP_HOST’].$modx->makeUrl($newsletterId);
$html = file_get_contents($url);
(Line 1479 mentioned in warning above)
$httpDom = ’http://’.$_SERVER[’HTTP_HOST’];
$html = preg_replace(’#(<img[^>]*src=\")([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);
$html = preg_replace(’#(<a[^>]*href=\")([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);
$html = preg_replace(’#(onclick=\"window\.open\(\’)([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);
return $html;
}[/tt]
I have absolutely no idea how to rewrite the TXNewsletters code to match the cURL code suggested by the host without wrecking havoc! Is anybody able to help me? Thanks!!