<ol start="1"> <li> <li><span><span>/**</span> </span></li> <li><span><span> * 發送post請求</span> </span></li> <li><span><span> * @param string post_data post鍵值對資料</span> </span></li> <li><span><span> * @return string</span> </span></li> <li><span><span> */</span> </span></li> <li> <span><span>函數</span> send_post($url </span> </li> <li><span> <span>$postdata</span> = http_build_query( <span>$post_data</span>); <span></span></span></li> <li>$選項<span> = </span>array</li>( <li> <span><span></span><span> </span>'meth od'</span> => </li>'發佈' <li>, <span><span></span> <span> </span>'標題'</span> => </li>'標題'<li> => '<span> => <span>$postdata</span>, <span></span> </span> </li> <li>' timeout'<span> => 15 * 60 <span>//逾時時間(單位:s)</span> <span></span></span> </li> <li> <span>$context<span> = stream_context_create(</span>$options<span>); </span> </span> </li> <li> <span>$結果<span> =</span>file_get_contents<span>(</span>$url</span>, false, </li>$context <li>回傳<span> <span>$結果</span>; <span></span></span> </li>}<li><span> </span></li> <li><span><span>//使用方法</span> </span></li> <li> <span><span> </span>'使用者名稱'<span> => </span>'stclair2201'</span>, </li> <li> <span><span> </span> '密碼'<span> => </span>'邯鄲'</span> </li> <li> <span><span>); </span><span></span></span>send_post(www),http</li> <li> <span></span>send_post( </li> <li> <span><span> </span><span></span> </span> </li> <li> <span></span> </li> <li> <span></span> </li> <li><span></span></li> <li><span></span></li>/**<li> <span></span> </li> <li><span> * Socket版本<span> </span></span></li> <li><span> * 使用方法:<span> ;</span> </span></li> <li><span><span> * request_by_socket('chajia8.com ', '/restServer.php', $post_string);</span> </span></li> <li><span><span> * /</span> </span></li> <li> <span><span>函式</span> request_by_socket($ho$re$m,$ </span>$埠</li> = 80,<li>$逾時<span> = 30) { <span></span></span> </li> <li>$socket<span> = <span>fsockopen</span>(<span>$re mote_server</span>,<span>fsockopen</span>(<span>$re mote_server</span>,<span>、$port</span>(<span>$rem </span>); </span> </li> <li> <span> <span>if </span> (!<span>$socket</span>) <span>die</span>(<span>"$errstr($errno)"</span>); <span></span><span> </span>); <span></span></span> </li> fwrite(<li>$socket<span>, <span>"使用者代理程式:套接字範例"</span>); <span></span><span></span> fwrite<span>_$socket</span></span> </li> fwrite ( <li> <span><span> fwrite(</span>$ socket<span>, </span>) + 8) . </span>""</li>); <li> <span><span></span> fwrite(<span>$socket</span>, </span>"接受:*/*" , </li>""<li>); <span> <span></span><span> fwrite(</span>$socket</span>, </li>"mypost=$post_string"<li>); <span><span></span> <span></span></span> </li>$header<li> = <span>""<span> ; </span><span></span><span> </span>while<span> (</span>$str<span> = trim(</span>fgets</span>(</li> <li>$header<span> .= <span>$ str</span>; <span></span></span> </li> } <li><span><span></span> <span></span></span></li> <li>$資料<span> = <span>""</span>; socket<span>)) { </span></span> </li> <li> <span>$data<span> .= </span>fgets <span>(</span>$socket</span>, 40096); </li> <li><span><span> } </span><span></span> </span></li> <li><span>} </span></li> <li><span>? > </span></li> <li><span> </span></li> <li><span></span></li> <li><span>/**</span></li> <li><span> * Curl版本 </span></li> <li><span> * 使用方法: </span></li> <li> <span> <span></span><span></span> * request_by_curl(</span>'http://www. qianyunlai.com/restServer.php'</li>, <li>$post_string<span>); <span></span><span></span> */ </span> </li> <li> <span></span>函數</li> request_by_curl(<li>$remote_p <span>$ ch<span> = curl_init(); </span><span></span><span> curl_setopt(</span>$ch</span>, CURLOPT_URL, </li>$remote_server<li>); <span><span></span></span> curl_setopt( </li>$post_string<li>); <span><span></span><span> curl_setopt(</span>$ch </span>, CURLOPT_RETURNTRANSFER, true); </li> <li> <span><span> curl_setopt(</span>$ch<span>, CURLOPT_USGENT, </span><span> </span>$data</span> = curl_exec(</li>$ch<li>); <span><span></span></span> curl_close(</li>$ch<li>); <span><span></span><span> </span></span> </li>} <li><span><span></span>? > <span></span></span></li> <li> <span>原網址:http://blog.sjzycxx.cn/post/435/<span></span></span> </li> 以上就介紹了PHP發送POST請求,包括了方面的內容,希望對PHP教程有興趣的朋友得到幫助。 <li> <span> </span> </li> </ol>