simplexml_load_file not working “php_network_getaddresses: failed: Name or service not known”

帅比萌擦擦* 提交于 2019-12-19 04:56:10

问题


I started getting these warnings just recently, im not sure why, nor do i know how to fix it. What confuses me more, it was working before, i started working on another class and now i starting getting this error...

im using linux fedora & apache for the webserver.

Warning: simplexml_load_file() [function.simplexml-load-file]: php_network_getaddresses: getaddrinfo failed: Name or service not known in GetImagesFlickr.php on line 17

Warning: simplexml_load_file(http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=9eb9f5e5fb72d6d2fed06cf4e64ba9bb&media=photos&per_page=50&text=fgdc) [function.simplexml-load-file]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in GetImagesFlickr.php on line 17

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=9eb9f5e5fb72d6d2fed06cf4e64ba9bb&media=photos&per_page=50&text=fgdc" in /var/www/html/yahoo/GetImagesFlickr.php on line 17

<?php
class GetImagesFlickr
{
    const API_KEY = "***"; // Flickr api key
    const MAX_RESULTS_RETURNED = 50; // Max results returned in search

    private $url; // Flickr webservice url

    public function __construct () 
    {
        $this->url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=".self::API_KEY."&media=photos&per_page=".self::MAX_RESULTS_RETURNED;
    }

    public function getImages ($search_term)
    {
        // Perform search and store xml data
        $xml_data = simplexml_load_file("{$this->url}&text={$search_term}");

        $search_results = array();

        // Go through xml data and store to array
        foreach ($xml_data->photos->photo as $current_photo)
        {
            $id = $current_photo['id'];
                        $title = $current_photo['title'];
            $farm = $current_photo['farm'];
            $secret = $current_photo['secret'];
            $server = $current_photo['server'];
            $url = "http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}.jpg";

            $complete_photo = array (
                'id'     => $id,
                                'title'  => $title,
                'farm'   => $farm,
                'secret' => $secret,
                'server' => $server,
                'url'    => $url
            );

            $search_results[] = $complete_photo;
        }

        return $search_results;
    }
}
?>

回答1:


You have a network/DNS problem, not a PHP problem. Seems your machine cannot resolve the IP of api.flickr.com.

"su" to your web server user and try to resolve the name there, i.e.

$ sudo bash
  # get root
$ su - apache
  # we're the apache user now
$ ping api.flickr.com


来源:https://stackoverflow.com/questions/6561769/simplexml-load-file-not-working-php-network-getaddresses-failed-name-or-servi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!