<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://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>https://jackxiang.com/post//</link>
<title><![CDATA[[原创]让file_get_contents() 函数也做post，以及两种写法！]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Fri, 06 Feb 2009 03:54:37 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	好像是由于我的curl_post有些问题，然后用到了如下函数的post：<br/>我的curl_post:<br/><div class="code">&nbsp;&nbsp;function curl_post($url, $content)<br/>&nbsp;&nbsp;&#123;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;$str_url = $url;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$str_post_data = $content;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$ch_curl = curl_init ();<br/>&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $ch_curl, CURLOPT_TIMEOUT, 3 );<br/>&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $ch_curl, CURLOPT_HEADER, false );<br/>&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $ch_curl, CURLOPT_POST, 1 );<br/>&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $ch_curl, CURLOPT_RETURNTRANSFER, true );<br/>&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $ch_curl, CURLOPT_URL, $str_url );<br/>&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $ch_curl, CURLOPT_POSTFIELDS, $str_post_data );<br/>&nbsp;&nbsp;&nbsp;&nbsp;$str_return = curl_exec ( $ch_curl );<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if ($str_return === false)<br/>&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;curl_close ( $ch_curl );<br/>&nbsp;&nbsp;&nbsp;&nbsp;return $str_return;<br/>&nbsp;&nbsp;&#125;</div><br/><br/>首先贴上生产环境的代码段【建鑫编写】：<br/><div class="code">&lt;?php<br/>function request_post($url,$data,&amp;$result)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$ctx = array (<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;http&#039; =&gt; array (<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;method&#039; =&gt; &#039;POST&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;header&#039;=&gt; &quot;Content-type: application/x-www-form-urlencoded&#92;r&#92;n&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;. &quot;Content-Length: &quot; . strlen($data) . &quot;&#92;r&#92;n&quot;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;content&#039; =&gt; $data<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $context = stream_context_create($ctx);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = file_get_contents($url,false,$context);<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ($result === false)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&#125;<br/>request_post(&quot;http://localhost&quot;,&quot;aa=aaa&quot;,$result);<br/>echo $result;<br/>?&gt;</div><br/>不过我对这个哥们用到ksort函数的file_get_contents()觉得不错：<br/><div class="code">function Post($url, $post = null)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$context = array();<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if (is_array($post))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ksort($post);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$context&#91;&#039;http&#039;&#93; = array<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;method&#039; =&gt; &#039;POST&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;content&#039; =&gt; http_build_query($post, &#039;&#039;, &#039;&amp;&#039;),<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;return file_get_contents($url, false, stream_context_create($context));<br/>&#125;<br/><br/>$data = array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;name&#039; =&gt; &#039;test&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;email&#039; =&gt; &#039;test@gmail.com&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;submit&#039; =&gt; &#039;submit&#039;,<br/>);<br/><br/>echo Post(&#039;http://www.url.com/to/submit.php&#039;, $data);</div><br/><br/><br/><br/><br/><br/><br/>转：<br/><br/><br/><br/>一直以为file_get_contents() 函数大概只能在请求一个url时做get操作，其实还有下面用法，可以让file_get_contents() 函数也做post：<br/><div class="code">&lt;?php<br/>// 1. 通过设置context来是file_get_contents()函数完成post操作，其实还可以设置很多的东西<br/>// 设置方法1 ================================================================<br/>$opts = array(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;http&#039;=&gt;array(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;method&#039;=&gt;&#039;POST&#039;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;content&#039;=&gt;&#039;a=b&amp;c=d&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;)<br/><br/>);<br/>$context = stream_context_create($opts);<br/><br/>// 设置方法2 ================================================================<br/>/**<br/>$context = stream_context_create(array()); // 参数必须是一个数组，允许是空数组<br/>stream_context_set_option ($context,&#039;http&#039;,&#039;method&#039;,&#039;POST&#039;); // 第一个参数必须是一个stream或由stream_context_create产生的一个stream的上下文<br/>stream_context_set_option ($context,&#039;http&#039;,&#039;content&#039;,&#039;a=b&amp;c=d&amp;e=f&#039;);<br/>*/<br/><br/>// =================================================================================<br/><br/>echo file_get_contents(&#039;http://ljj.cn/test.php&#039;,false,$context);<br/><br/>exit;<br/>?&gt; </div><br/><br/><br/>file_get_content post的第一种写法：<br/><br/>$data = "ms=BE&iso=BE&vat=0462569145&BtnSubmitVat=Verify";//参数<br/>$opts = array('http' => array (<br/>'method'=>"POST",<br/>'header'=>"Accept-language: en&#92;r&#92;n" .<br/>"Content-type: application/x-www-form-urlencoded&#92;r&#92;n".<br/>"Content-Length: " . strlen($data) . "&#92;r&#92;n",<br/>'content' => $data<br/>)<br/>);<br/>//file_get_content第三个参数是资源类型，所以要先把数组转换成资源类型<br/>//stream_context_create 第一个参数必须是二唯数组，第二个数组必须是一唯数组<br/>$context = stream_context_create($opts);<br/>$content = file_get_contents('xxxx', false, $opts);<br/>echo $content;<br/>urlencode() 如果url地址有空格，就要使用<br/><br/><br/>file_get_content post的第二种写法：<br/>$data = array(<br/>'ms'=>'BE',<br/>'iso'=>'BE',<br/>'vat'=>'0462569145',<br/>'BtnSubmitVat'=>'Verify',<br/>);<br/><br/>$options = array('http'=>array(<br/>'method'=>"POST",<br/>'header'=><br/>"Accept-language: en&#92;r&#92;n".<br/>"Content-type: application/x-www-form-urlencoded&#92;r&#92;n",<br/>'content'=>http_build_query($data)//http_build_query是把数组转换为字符传的形式.结果形式：ms=BE&iso=BE&vat=0462569145&BtnSubmitVat=Verify,版本为php5才有<br/>));<br/>$context = stream_context_create($options);<br/>$content = file_get_contents('xxx', false, $context);<br/><br/>file_get_content get的写法：<br/>$content = file_get_contents('xxx?ms=BE&iso=BE&vat=0462569145&BtnSubmitVat=Verify');<br/>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [原创]让file_get_contents() 函数也做post，以及两种写法！]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>