-2

Our url is remote url.i can run through Ip address.it thrown error as "file_get_contents failed to open stream: Connection refused".I have use this code.

code:

$html = file_get_contents('http://xx.xxx.xx.xx:xxxx/apps/index.php');
print_r($html);
var_dump($html); 

What does this error?

samarth
  • 87
  • 1
  • 1
  • 7
  • it working in local and any pc but it not working on godaddy server – samarth Feb 11 '15 at 09:17
  • 1
    possible duplicate of [file\_get\_contents() connection refused for my own site](http://stackoverflow.com/questions/3283995/file-get-contents-connection-refused-for-my-own-site) – Lorenz Meyer Feb 11 '15 at 09:19
  • @Lorenz Meyer, m not getting that point. – samarth Feb 11 '15 at 09:24
  • but i have connect to remote server url then error display for file_get_contents failed to open stream: Connection refused – samarth Feb 11 '15 at 09:52

2 Answers2

2

A lot of hosts will prevent you from loading files from remote URLs for security reasons (allow_url_fopen setting in php.ini). It's better to use CURL to download the contents of the file.

<?php
$url = 'http://xx.xxx.xx.xx:xxxx/apps/index.php';
$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);

Ref: Get file content via PHP cURL

HTH :)

Community
  • 1
  • 1
1

Getting a page would require you to open a stream. Something like this:

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>

For reference please check PHP.net: http://php.net/manual/en/function.file-get-contents.php

djkevino
  • 264
  • 2
  • 16