杰奇1.7 开启SSL后台生成报错

fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /www/wwwroot/xiaoshuo/modules/article/class/package.php on line 1603 Warning: fsockopen() [function.fsockopen]: unable to connect to www.xiaoshuo.cnhttps:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /www/wwwroot/xiaoshuo/modules/article/class/package.php on line 1603

报错如上,根据提示修改文件/www/wwwroot/xiaoshuo/modules/article/class/package.php

function jieqi_socket_url($url){
	if(!function_exists('fsockopen')) return false;
	$method = "GET";
	$url_array = parse_url($url);
	$port = isset($url_array['port'])? $url_array['port'] : 80;
	$fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30);
	if(!$fp) return false;
	$getPath = $url_array['path'];
	if(!empty($url_array['query'])) $getPath .= "?". $url_array['query'];
	$header = $method . " " . $getPath;
	$header .= " HTTP/1.1\r\n";
	$header .= "Host: ". $url_array['host'] . "\r\n"; //HTTP 1.1 Host域不能省略
	/*
	//以下头信息域可以省略
	$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 \r\n";
	$header .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,q=0.5 \r\n";
	$header .= "Accept-Language: en-us,en;q=0.5 ";
	$header .= "Accept-Encoding: gzip,deflate\r\n";
	*/
	$header .= "Connection:Close\r\n\r\n";
	fwrite($fp, $header);
	if(!feof($fp)) fgets($fp, 8);
	//while(!feof($fp)) echo fgets($fp, 128);
	fclose($fp);
	return true;
}



修改为

function jieqi_socket_url($url) {    if (!function_exists('fsockopen')) return false;    // 过滤掉错误格式中的 https 关键字
    $url = preg_replace('/https/', '', $url);    // 确保 URL 的格式正确
    if (!preg_match('/^(http|https):\/\//', $url)) {        $url = 'http://' . $url;
    }    $method = "GET";    $url_array = parse_url($url);    // 设置默认端口为 80,如果是 https 则设置为 443
    $port = isset($url_array['port']) ? $url_array['port'] : (isset($url_array['scheme']) && $url_array['scheme'] == 'https' ? 443 : 80);    // 为 https 协议使用 ssl:// 前缀
    $host = isset($url_array['scheme']) && $url_array['scheme'] == 'https' ? 'ssl://' . $url_array['host'] : $url_array['host'];    // 打开套接字连接
    $fp = fsockopen($host, $port, $errno, $errstr, 30);    if (!$fp) return false;    // 构建请求路径
    $getPath = isset($url_array['path']) ? $url_array['path'] : '/';    if (!empty($url_array['query'])) $getPath .= "?" . $url_array['query'];    // 构建请求头
    $header = "$method $getPath HTTP/1.1\r\n";    $header .= "Host: " . $url_array['host'] . "\r\n";    $header .= "Connection: Close\r\n\r\n";    // 发送请求
    fwrite($fp, $header);    // 读取响应的开始部分
    if (!feof($fp)) fgets($fp, 8);    fclose($fp);    return true;
}


改动点:

  • 过滤 https:通过 preg_replace('/https/', '', $url) 过滤掉错误格式中的 https 部分。

  • 检查和设置 URL 协议:确保 URL 包含协议(http 或 https)。如果没有,则默认添加 http://。

  • 处理端口:根据协议设置默认端口,如果是 https,则默认端口为 443;否则为 80。

  • 处理 https 协议:如果协议是 https,在 fsockopen 调用中使用 ssl:// 前缀。


注意:

  • URL 验证:这个处理方法会过滤掉 https 关键字,但仍然需要确保 URL 的其他部分(如域名和路径)格式正确。

  • 错误处理:这个函数简单地返回 false 当连接失败,这在生产环境中可能需要更多的错误处理和日志记录。