PHP发送POST请求
<p>/**</p><p> * 发送post请求</p>
<p> * @param string $url 请求地址</p>
<p> * @param array $post_data post键值对数据</p>
<p> * @return string</p>
<p> */</p>
<p>function send_post($url, $post_data) { </p>
<p> $postdata = http_build_query($post_data);</p>
<p> $options = array(</p>
<p> 'http' => array(</p>
<p> 'method' => 'POST',</p>
<p> 'header' => 'Content-type:application/x-www-form-urlencoded',</p>
<p> 'content' => $postdata,</p>
<p> 'timeout' => 15 * 60 // 超时时间(单位:s)</p>
<p> )</p>
<p> );</p>
<p> $context = stream_context_create($options);</p>
<p> $result = file_get_contents($url, false, $context);</p>
<p> return $result;</p>
<p>} </p>
<p> </p>
<p>//使用方法</p>
<p>$post_data = array(</p>
<p> 'username' => 'stclair2201',</p>
<p> 'password' => 'handan'</p>
<p>);</p>
<p>send_post('http://www.jb51.net', $post_data);</p>
页:
[1]