<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[个人原创]Curl模拟Host和Ip对应关系实现POST和GET请求，不用设置Host文件也可直接访问对应的虚拟机和IP对应位置。以及如何模拟客户端IP的方法。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Fri, 01 Jul 2011 11:56:43 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：http://jackxiang.com/post/2656/&nbsp;&nbsp;里有通过php进行curl配置并进行模拟IP来进行请求：PHP的curl扩展为我们用了CURLOPT_HTTPHEADER来做host的工作， curl -l -H &quot;Host:test3.qq.com&quot;&nbsp;&nbsp;http://17.2*.*.70/index.php，这儿讲一下用shell下的curl来一行进行模拟及其用wireshark进行抓包发现其http的头的原理。自己之前写过放在这儿：jackxiang.com/post/2656/<br/>____________实践有用的Curl强大功能________________<br/>用Curl直接模拟Host方法，Curl伪造Refer的方法:<br/>http://jackxiang.com/post/6201/<br/>获取cookie后，模拟refer并带上cookie通过curl访问某个接口：<br/>http://jackxiang.com/post/4082/<br/>PHP用CURL伪造IP和来源：<br/>http://jackxiang.com/post/6201/<br/>______________________________________________<br/>参考：http://get.ftqq.com/9091.get<br/>完整示例只传入data，其它项默认即可：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;function curl_post($data,$url=&quot;http://101.200.228.***/sendmail.php&quot;,$host=array(&quot;Host: *.jackxiang.com&quot;),$refer=&quot;wx.*.com&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ch = curl_init();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res= curl_setopt ($ch, CURLOPT_URL,$url);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_REFERER, $refer);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ($ch, CURLOPT_HEADER, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_POST, 1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch,CURLOPT_HTTPHEADER,$host);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = curl_exec ($ch);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_close($ch);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($result == NULL) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $result;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

</textarea><br/>调用示例：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(isset($sendEmailArr) &amp;&amp; ($sendEmailArr != NULL))&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$serialize=serialize($sendEmailArr);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data = &quot;neworders=&quot;.$serialize;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $retInfo = $this-&gt;curl_post($data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$date = date(&quot;Y-m-d H:i:s&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;提示:没有发现有订单,不用发邮件通知。&#123;$date&#125;&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
</textarea><br/><br/><br/>实现GET请求成功，注意 header是数组，参考来自：http://get.ftqq.com/9091.get&nbsp;&nbsp;，关于Host: xxx.xx.com在Header里是数组,否则会报警告：<br/>第三行改为<br/>$header[] = &quot;Content-type: text/xml&quot;; // 改为数组解决<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$setEggsHostArr = array(Config::getInstance()-&gt;getConf(&#039;SETEGGSHOST&#039;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$setEggsHttp = Config::getInstance()-&gt;getConf(&#039;SETEGGSHTTP&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$setEggsPara = Config::getInstance()-&gt;getConf(&#039;SETEGGSPARA&#039;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$setEggsHttp = sprintf($setEggsHttp,$eidsIpBind);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$setEggsPara = sprintf($setEggsPara,$eid,$temp,$humi);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$rsJsonStr = $model-&gt;curlGetByHost($setEggsHostArr,$setEggsPara,$setEggsHttp);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$rsArrOut = json_decode($rsJsonStr,true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $this-&gt;writeJson($rsArrOut[&#039;code&#039;], [],$rsArrOut[&#039;msg&#039;]);
</textarea><br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;public function curlGetByHost($host,$data,$url)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ch = curl_init();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$res= curl_setopt ($ch, CURLOPT_URL,$url);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var_dump($res);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ($ch, CURLOPT_HEADER, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_POST, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch,CURLOPT_HTTPHEADER,$host);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = curl_exec ($ch);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_close($ch);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($result == NULL) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $result;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
</textarea><br/>配置文件：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;SETEGGSHOST&#039; =&gt; &#039;Host: wx.xxx.com&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;SETEGGSHTTP&#039; =&gt; &#039;http://%s/Api/Eggs/Eggs/doSetTemp&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;SETEGGSPARA&#039; =&gt; &#039;eid=%s&amp;temp=%s&amp;humi=%s&#039;,<br/><br/>最后，Host: wx.xx.com&nbsp;&nbsp; =数组传入=》 $setEggsHostArr = array(Config::getInstance()-&gt;getConf(&#039;SETEGGSHOST&#039;));得注意。<br/>Host: wx.xx.com&nbsp;&nbsp; eid=83419527&amp;temp=38.2&amp;humi=45&nbsp;&nbsp;&nbsp;&nbsp;http://wx.xx.com/Api/Eggs/Eggs/doSetTempbool<br/>—————————————————————Curl模拟Host和Ip对应关系—————————————————————<br/>一个是Linux命令行来实现，二是从PHP编码上来实现不用修改Linux的Host就可能实现对虚拟主机的访问，实现如下：<br/>一.假如70上有一个test3.qq.com ,我们在Windows下还需要通过<br/><textarea name="code" class="html" rows="15" cols="100">
editplus C:&#92;WINDOWS&#92;system32&#92;drivers&#92;etc&#92;hosts
</textarea><br/>对Host加入配置：<br/><textarea name="code" class="html" rows="15" cols="100">
17.2*.*.70&nbsp;&nbsp;test3.qq.com
</textarea><br/>后，<br/>才能通过浏览器访问，而我们如果不加就不能访问。<br/>现在，可以通过Curl来不用修改host文件也能访问，如下：<br/>去Host：<br/><textarea name="code" class="html" rows="15" cols="100">
#17.2*.*.70&nbsp;&nbsp;test3.qq.com
</textarea><br/>用Curl直接模拟Host如下：<br/><textarea name="code" class="html" rows="15" cols="100">
curl -l -H &quot;Host:test3.qq.com&quot;&nbsp;&nbsp;http://17.2*.*.70/index.php
</textarea><br/>如果看代码图片：<br/>curl -l -H &quot;Host:mytv.jackxiang.com&quot;&nbsp;&nbsp;http://127.0.0.1/uploads/temp/201310261334109440.jpg &gt; xdy.jpg<br/>-----------------------------------------------------------------------------------------------------------------------------------------------------------<br/>如果有&amp;号连接的Url需要对Url加引号：<br/>curl -l --silent -v -H &quot;Host:api.xdxp.cn&quot;&nbsp;&nbsp;&quot;http://115.182.35.108/general/getVideoCategoryList?uid=100&amp;videoCategoryIds=15,16,17,18,19,20,31&amp;format=json&quot;<br/>*&nbsp;&nbsp; Trying 115.182.35.108... connected<br/>* Connected to 115.182.35.108 (115.182.35.108) port 80 (#0)<br/>&gt; GET /general/getVideoCategoryList?uid=100&amp;videoCategoryIds=15,16,17,18,19,20,31&amp;format=json HTTP/1.1<br/><br/>不加引号的情况：<br/>curl -l --silent -v -H &quot;Host:api.xdxp.cn&quot;&nbsp;&nbsp;http://115.182.35.108/general/getVideoCategoryList?uid=100&amp;videoCategoryIds=15,16,17,18,19,20,31&amp;format=json<br/>* About to connect() to 115.182.35.108 port 80 (#0)<br/>*&nbsp;&nbsp; Trying 115.182.35.108... connected<br/>* Connected to 115.182.35.108 (115.182.35.108) port 80 (#0)<br/>&gt; GET /general/getVideoCategoryList?uid=100 HTTP/1.1<br/>-----------------------------------------------------------------------------------------------------------------------------------------------------------<br/><br/>与此同时，我们还能通过在Linux自己带的Curl来进行不用设置/etc/hosts来实现我们自己的模拟host访问。这样在调试接口等就不用我们自己每次都修改Host文件了,带来了极大的方便。<br/><br/>二.我们在PHP代码编程使用中PHP设置Curl参数来实现的代码片段如下,注意传入的是IP，而Host设置在函数的代码中：<br/><br/><textarea name="code" class="html" rows="15" cols="100">function curl_post($data,$host = &quot;http://116.255.139.240/user_join/freereg.php??from_id=10537&quot;)
&#123;
&nbsp;&nbsp;$ch = curl_init();
&nbsp;&nbsp;$res= curl_setopt ($ch, CURLOPT_URL,$host);
&nbsp;&nbsp;curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
&nbsp;&nbsp;curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
&nbsp;&nbsp;curl_setopt ($ch, CURLOPT_HEADER, 0);
&nbsp;&nbsp;curl_setopt($ch, CURLOPT_POST, 1);
&nbsp;&nbsp;curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
&nbsp;&nbsp;curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
&nbsp;&nbsp;curl_setopt($ch,CURLOPT_HTTPHEADER,array(&quot;Host: jackxiang.com&quot;));

&nbsp;&nbsp;$xyz = curl_exec ($ch);
&nbsp;&nbsp;if ($xyz == NULL) 
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;return $xyz;
&#125;</textarea><br/>如一个域名有两个IP，如何来实现调用的简单实现，这样有利于分担接口负载，如下：<br/><textarea name="code" class="html" rows="15" cols="100">
//调用方法
 $data = &quot;uin=372647693&amp;appkey=778899&quot;;
 $host[1] = &quot;http://10.6.207.110/user_join/freereg.php??from_id=10537&quot;;
 $host[2] = &quot;http://116.255.139.240/user_join/freereg.php??from_id=10537&quot;;
 $randval = rand(1,2);&nbsp;&nbsp;
 $host = $host[$randval];
 curl_post($data,$host);</textarea><br/><br/>综上所述，其本质是模拟了Http协议的头，想了解更多请看我之前写的一篇文章：<br/>《PHP CURL CURLOPT_HTTPHEADER设置HOST（也就是不用配置host访问），Curl伪造Refer的方法及其杂谈》<br/>Url：<a href="http://jackxiang.com/post/4022/" target="_blank">http://jackxiang.com/post/4022/</a><br/><br/><br/>—————————————————————伪造客户端IP—————————————————————<br/>一）这就是伪造的Curl的PHP代码：<br/>curl_setopt($ch, CURLOPT_HTTPHEADER, array(&#039;X-FORWARDED-FOR:&#039;.$sendip, &#039;CLIENT-IP:&#039;.$sendip));<br/><textarea name="code" class="php" rows="15" cols="100">
function get($user_agent = false,$sendip=&#039;&#039;) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ch = curl_init($this-&gt;_url);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//伪造客户端IP
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if($sendip!=&quot;&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_setopt($ch, CURLOPT_HTTPHEADER, array(&#039;X-FORWARDED-FOR:&#039;.$sendip, &#039;CLIENT-IP:&#039;.$sendip));
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($user_agent) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_USERAGENT, &quot;Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!empty($this-&gt;_cookie))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_COOKIE, $this-&gt;_cookie);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = curl_exec($ch);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_close($ch);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $result;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
 
&#125;
?&gt;
</textarea><br/>来自：http://www.phpec.org/php/curl.html<br/>参考：http://www.onlypo.com/archives/7<br/><br/><br/>二）在Linux的Shell环境下如何不用PHP设置Header呢？<br/>linux curl http header处理：<br/><br/>设置http请求头信息：<br/>curl -A &quot;Mozilla/5.0 Firefox/21.0&quot; http://www.baidu.com #设置http请求头User-Agent<br/>curl -e &quot;http://pachong.org/&quot; http://www.baidu.com #设置http请求头Referer<br/>curl -H &quot;Connection:keep-alive &#92;n User-Agent: Mozilla/5.0&quot; http://www.aiezu.com<br/>来自：http://www.aiezu.com/system/linux/linux_curl_syntax.html<br/><br/>于是，如果忽悠下服务器端简单获取客户端的IP，实践如下,用-H参数即可：<br/><textarea name="code" class="php" rows="15" cols="100">
curl -H &quot;X-Forwarded-For:192.168.1.108&quot; &quot;http://jackxiang.com/album/getAlbumsByUserId?userid=100081&amp;format=xml&amp;appkey=5424371297&amp;checkOauth=0dac14fbc490e2deXXXXX15c1c42&amp;pid=108&quot;
</textarea><br/>于是日志显示：2013/12/06 21:05:57&nbsp;&nbsp;&nbsp;&nbsp; HostIp:jackxiang.com--ClientIp:192.168.1.108-- <br/>果然被curl给忽悠了吧，其被忽悠的代码如下：<br/>X-Forwarded-For:192.168.1.108 在http头里通过linux下的curl注入头里即可！<br/>调用如下：<br/>file_put_contents($logFile, date(&quot;Y/m/d H:i:s&quot;) . &quot;&#92;t&quot; . $type .&#039;HostIp:&#039;.$this-&gt;request-&gt;getHttpHost().&#039;--&#039;.&#039;ClientIp:&#039;.$this-&gt;request-&gt;getClientIp().&#039;--&#039;. &quot;&#92;t&quot; . var_export($msg, true) . &quot;&#92;n&quot;, FILE_APPEND);<br/>代码片段如下：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp; * Get the HTTP host.
&nbsp;&nbsp;&nbsp;&nbsp; *
&nbsp;&nbsp;&nbsp;&nbsp; * &quot;Host&quot; &quot;:&quot; host [ &quot;:&quot; port ] ; Section 3.2.2
&nbsp;&nbsp;&nbsp;&nbsp; * Note the HTTP Host header is not the same as the URI host.
&nbsp;&nbsp;&nbsp;&nbsp; * It includes the port while the URI host doesn&#039;t.
&nbsp;&nbsp;&nbsp;&nbsp; *
&nbsp;&nbsp;&nbsp;&nbsp; * @return string
&nbsp;&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp;&nbsp;&nbsp;public function getHttpHost()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$host = $this-&gt;getServer(&#039;HTTP_HOST&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (! empty($host)) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $host;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$scheme = $this-&gt;getScheme();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$name = $this-&gt;getServer(&#039;SERVER_NAME&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$port = $this-&gt;getServer(&#039;SERVER_PORT&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (($scheme == self::SCHEME_HTTP &amp;&amp; $port == 80) &#124;&#124; ($scheme == self::SCHEME_HTTPS &amp;&amp; $port == 443)) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $name;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $name . &#039;:&#039; . $port;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp; * Get the Client IP.
&nbsp;&nbsp;&nbsp;&nbsp; *
&nbsp;&nbsp;&nbsp;&nbsp; * @return s
&nbsp;&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp;&nbsp;&nbsp;public function getClientIp ()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$clientIp = &#039;&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (isset($_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;])) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$clientIp = $_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif (isset($_SERVER[&#039;HTTP_CLIENT_IP&#039;])) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$clientIp = $_SERVER[&#039;HTTP_CLIENT_IP&#039;];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif (isset($_SERVER[&#039;REMOTE_ADDR&#039;])) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$clientIp = $_SERVER[&#039;REMOTE_ADDR&#039;];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $clientIp;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

</textarea><br/><br/>模拟修改Host的IP,不用修改Host文件的方法也是-H参数，能否合并起来使用呢？实践是不行的，如下探讨研究：<br/><textarea name="code" class="php" rows="15" cols="100">
 curl -H &quot;Host:jackxiang.com&quot; &quot;http://127.0.0.1/album/getAlbumsByUserId?userid=18073494&amp;format=xml&amp;appkey=597&amp;checkOauth=0dac14fbc490e2def12110fd215c1c42&amp;pid=100&quot;
</textarea><br/>但是把上面两个写在一块用&#92;n分开，实践好像不行，尽管上面有这样一个示例：<br/>curl -H &quot;Connection:keep-alive &#92;n User-Agent: Mozilla/5.0&quot; http://www.aiezu.com<br/>实践是可行的，如：curl -H &quot;Connection:keep-alive &#92;n User-Agent: Mozilla/5.0&quot; http://www.baidu.com<br/>Connection：keep-alive &#92;n User-Agent: Mozilla/5.0<br/>但是把Curl模拟Host所对IP及如模拟客户端IP放一块，实践不Ok，如下：<br/><textarea name="code" class="php" rows="15" cols="100">
 curl -H &quot;Host: jackxiang.com &#92;n X-Forwarded-For: 192.168.1.108&quot;&nbsp;&nbsp;&quot;http://127.0.0.1/album/getAlbumsByUserId?userid=18073494&amp;format=xml&amp;appkey=597&amp;checkOauth=0dac14fbc490e2def12110fd215c1c42&amp;pid=100&quot;
</textarea><br/>出现接口返回不对，Nginx如下，只能单用吗？疑惑，因为这都是&#92;r&#92;n拼接的呀，&#92;r&#92;n也不行，如：http://www.myhack58.com/Article/html/3/62/2012/34944_2.htm<br/>&lt;html&gt;<br/>&lt;head&gt;&lt;title&gt;403 Forbidden&lt;/title&gt;&lt;/head&gt;<br/>&lt;body bgcolor=&quot;white&quot;&gt;<br/>&lt;center&gt;&lt;h1&gt;403 Forbidden&lt;/h1&gt;&lt;/center&gt;<br/>&lt;hr&gt;&lt;center&gt;nginx&lt;/center&gt;<br/>&lt;/body&gt;<br/>&lt;/html&gt;<br/>很有可能是一个要非ip的字母数字作为域名，而另一个是要ip冲突导致矛盾吧？<br/><br/>Add Time：2013-12-19<br/>在实践中用到，一外包兄弟写的代码经过队列调时出现没有入库，但是线上有多台机器，而所配置的机器的/etc/hosts指向是虚拟IP，不能修改，于是否，用这个方法找到了问题所在，其双方沟通的接口参数不一致导致，当时是怎么说通了，这个真是玄乎，代码如下：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$postData = array (
&nbsp;&nbsp;&#039;data&#039; =&gt;
&nbsp;&nbsp;array(
&nbsp;&nbsp; &#039;uuid&#039; =&gt; &#039;05ee5fd7-6887-11e3-bf9b-b9856d949d58&#039;,
&nbsp;&nbsp; &#039;title&#039; =&gt; &#039;7岁小孩酒瓶上做俯卧撑蹿红网络&#039;,
&nbsp;&nbsp; &#039;description&#039; =&gt; &#039;7岁小孩酒瓶上做俯卧撑蹿红网络&#039;,
&nbsp;&nbsp; &#039;tags&#039; =&gt; &#039;11&#039;,
&nbsp;&nbsp; &#039;category_id&#039; =&gt; &#039;101&#039;,
&nbsp;&nbsp; &#039;time_span&#039; =&gt; &#039;15&#039;,
&nbsp;&nbsp; &#039;user_id&#039; =&gt; &#039;31746864&#039;,
&nbsp;&nbsp; &#039;is_original&#039; =&gt; &#039;0&#039;,
&nbsp;&nbsp; &#039;partner_id&#039; =&gt; &#039;b74a99&#039;,
&nbsp;&nbsp; &#039;event_id&#039; =&gt; &#039;208&#039;,
&nbsp;&nbsp; &#039;thumb_path&#039; =&gt; &#039;http://p3.img.cctvpic.com/xiyou/thumb/2013/12/19/16/05ee5fd7-6887-11e3-bf9b-b9856d949d58-thumb.jpg&#039;,
 ),
&nbsp;&nbsp;&#039;url&#039; =&gt; &#039;http://jackxiang.com/mytv/&#039;,
&nbsp;&nbsp;&#039;action&#039; =&gt; &#039;video&#039;,
&nbsp;&nbsp;&#039;type&#039; =&gt; &#039;approvedvideo&#039;,
);

//$URL=$postData[&#039;url&#039;];
$URL=&quot;http://127.0.0.1/mytv/&quot;;
$PostFileds = &quot;action=&quot;.$postData[&#039;action&#039;].&quot;&amp;type=&quot;.$postData[&#039;type&#039;].&quot;&amp;data=&quot;.urlencode(json_encode($postData[&#039;data&#039;]));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostFileds);
curl_setopt($ch,CURLOPT_HTTPHEADER,array(&quot;Host:jackxiang.com&quot;));
curl_exec ($ch);
curl_close ($ch);
</textarea><br/><br/>对下面这个链接进行tcpdump抓包查看其请求是怎么样的，这儿python主要是想对json输出不能轻易读取加上json输出一行进行格式化：<br/><textarea name="code" class="php" rows="15" cols="100">
curl -H&quot;Host:t43.jackxiang.com&quot; &quot;http://127.0.0.1/json.php&quot; &#124; python -mjson.tool
</textarea><br/>第一点：直接用ip访问就不用找dns了，直接对IP访问，这是一定的。<br/>第二点：这个-H主要体现在头上，也就是服务器（如：WebServer nginx apache tomcat都是对这个解析进行分发到或是php-fpm进行解析这个头作返回）<br/>User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5<br/>Accept: */*<br/>Host:t43.jackxiang.com<br/><br/>也就是说，根据nginx里的conf文件进行分发时核对Host，或是php-fpm进行分发，这块还需要进一步研究，自己写server时可能会用到，也就这儿可以作为标志，至于怎么去的，我们可以直接用域名作对比：<br/>1）域名直接访问（会先读host或dns得到一个ip后直接对其发起连接），curl &quot;http://t43.jackxiang.com/json.php&quot;：<br/>Source&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Destination&nbsp;&nbsp;&nbsp;&nbsp;Protocol<br/>192.168.131.6&nbsp;&nbsp;&nbsp;&nbsp;115.182.9.243&nbsp;&nbsp;&nbsp;&nbsp;HTTP<br/><br/>GET /json.php HTTP/1.1<br/>User-Agent: curl/7.17.0 (i586-pc-mingw32msvc) libcurl/7.17.0 zlib/1.2.2<br/>Host: t43.jackxiang.com<br/>Accept: */*<br/><br/>附：curl -H&quot;Host:t43.jackxiang.com&quot; &quot;http://115.182.9.243/json.php&quot;<br/>2）用-H的curl通过localhost进行访问：<br/>Source&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Destination&nbsp;&nbsp;&nbsp;&nbsp;Protocol<br/>127.0.0.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 127.0.0.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HTTP<br/>User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5<br/>Accept: */*<br/>Host:t43.jackxiang.com<br/><br/><br/>直接访问IP是不行的，加一个错误的头也是不行的，说明的的确确是这个Host：xxx进行解析：<br/><br/>C:&#92;&gt;curl -H&quot;t43.jackxiang.com&quot; &quot;http://115.182.9.243/json.php&quot;<br/>&lt;html&gt;<br/>&lt;head&gt;&lt;title&gt;404 Not Found&lt;/title&gt;&lt;/head&gt;<br/>&lt;body bgcolor=&quot;white&quot;&gt;<br/>&lt;center&gt;&lt;h1&gt;404 Not Found&lt;/h1&gt;&lt;/center&gt;<br/>&lt;hr&gt;&lt;center&gt;nginx&lt;/center&gt;<br/>&lt;/body&gt;<br/>&lt;/html&gt;<br/><br/>C:&#92;&gt;curl -H&quot;Host:t43.jackxiang.com&quot; &quot;http://115.182.9.243/json.php&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;&nbsp;&nbsp;&quot;status&quot;:200,&nbsp;&nbsp;&quot;data&quot;:&nbsp;&nbsp;[&nbsp;&nbsp;&#123;&nbsp;&nbsp;&quot;id&quot;:1000,&nbsp;&nbsp;&quot;name&quot;:&quot;John&quot;&nbsp;&nbsp;&#125;,&nbsp;&nbsp;&#123;&nbsp;&nbsp;&quot;id&quot;:1004<br/>,&nbsp;&nbsp;&quot;name&quot;:&quot;Tom&quot;&nbsp;&nbsp;&#125;&nbsp;&nbsp;]&nbsp;&nbsp;&#125;<br/><br/>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [个人原创]Curl模拟Host和Ip对应关系实现POST和GET请求，不用设置Host文件也可直接访问对应的虚拟机和IP对应位置。以及如何模拟客户端IP的方法。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>