- <?php
- /*
- * 在代码里面设定url访问对应ip的服务器
- * 适用于lvs等集群服务器的管理
- * 根据需要,可自行添加返回值
- */
- url_bind_ip('http://www.xxx.com/index.php?aa=123','192.168.10.23'); // 这是示例
- function url_bind_ip($url, $ip = null) {
- $url = parse_url($url);
- if (!isset($url['port'])) {
- if ($url['scheme'] == 'http'){
- $url['port'] = 80;
- } else if ($url['scheme'] == 'https'){
- $url['port'] = 443;
- }
- }
- $url['query'] = isset($url['query'])?$url['query']:'';
- $url['protocol'] = $url['scheme'].'://';
- $eol="\\r\\n";
- $headers = 'GET '.$url['protocol'].$url['host'].$url['path'].'?'.$url['query'].' HTTP/1.1'.$eol.
- 'Host: '.$url['host'].$eol.
- 'Content-Length: '.strlen($url['query']).$eol.
- $eol.$url['query'];
- $fp = fsockopen($ip ? $ip : $url['host'], $url['port'], $errno, $errstr, 5);
- if ($fp) {
- fwrite($fp, $headers);
- }
- }
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/260920136166.html
来源: http://www.codesnippet.cn/detail/260920136166.html