fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known

$url=$row1['content_url'];
$file_handle = fopen($url,"r");
$line=fread($file_handle,400);
$line1 = wordwrap($line,400,"</br>" );
fclose($file_handle);

the above give is my code for reading data in the text file... it is working fine in my localhost... But it is not working in serevr.....

Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/content/08/11968108/html/dev/index.php on line 57

Warning: fread() expects parameter 1 to be resource, boolean given in /home/content/08/11968108/html/dev/gopi/index.php on line 58

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/content/08/11968108/html/dev/gopi/index.php on line 60

Answers


Make sure the URL in $url contains a wrapper (like http).

Explanation

fopen("www.domain.com","r");

This will look for a local file named www.domain.com

fopen("http://www.domain.com","r");

This will try to get the external file http://www.domain.com (if allow_url_fopen is enabled)

But your error could also indicate an old cached DNS record. If you can, try to change your name server on the production system (e.g.: 8.8.8.8 for the name server from google).


If you just need to handle the warnings that are thrown (as I did), you may just do this to turn them into Exceptions that can be handled:

set_error_handler(
  function ($err_severity, $err_msg, $err_file, $err_line, array $err_context) {
    // do not throw an exception if the @-operator is used (suppress)
    if (error_reporting() === 0) return false;

    throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
  },
  E_WARNING
);
try {
  $fp = fopen("http://example.com","r");
} catch (Exception $e) {
  echo $e->getMessage();
}
restore_error_handler();

Solution based on this thread/question.


Need Your Help

extract URL from dropbox response body

javascript node.js dropbox-api

I am using the dropbox core api to upload files and then generate shareable links for those files. I want to extract the generated shareable link from the response body but I am unable to do so. The

Unable to bind Component attribute with controller

salesforce apex-code visualforce

I am trying to develop a visualforce custom component which takes an attribute from a visual force page. I need to access that attribute in controller's Constructor so that i can brings some record...