简体   繁体   中英

module prestashop can not run file_get_contents

I create module and using file_get_content function but can not success.

function openURL($url, $use_include_path = false, $stream_context = null, $curl_timeout = 500)
{
    if ($stream_context == null && preg_match('/^https?:\/\//', $url)) {
        $stream_context = @stream_context_create(array('http' => array('timeout' => $curl_timeout)));
    }
    print_r($stream_context);
    if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url)) {
        return file_get_contents($url, $use_include_path, $stream_context);
    } elseif (function_exists('curl_init')) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_TIMEOUT, $curl_timeout);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        if ($stream_context != null) {
            $opts = stream_context_get_options($stream_context);
            if (isset($opts['http']['method']) && Tools::strtolower($opts['http']['method']) == 'post') {
                curl_setopt($curl, CURLOPT_POST, true);
                if (isset($opts['http']['content'])) {
                    parse_str($opts['http']['content'], $post_data);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
                }
            }
        }
        $content = curl_exec($curl);
        curl_close($curl);
        return $content;
    } else {
        return false;
    }
}

There is an error with filet_get_content function:

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known

Please help Thanks

It means your server cannot connect to the outside world

This probably won't change anything given the DNS issues

So, If you have permission, try changing the name servers in your /etc/resolv.conf file to other nameservers.

or

There should be as well httpd allowed to connect outside. Check you selinux policy. this helps me to solve connection problem:

setsebool -P nis_enabled 1 setsebool -P httpd_can_network_connect 1

You have to double check two things :

  • Server (Firewall and DNS) configuration.
  • Php configuration (maybe file_get_contents and curl are blocked)

Although, to respect Prestashop's standards, you should use Tools::file_get_contents(), which will wrap your request around a debugged function.

Please check the url from which you are getting the content. May be targeted url is not working or not exist?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM