php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法
更新時間:2015年06月02日 10:29:09 作者:不吃皮蛋
這篇文章主要介紹了php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法,涉及php使用socket傳輸數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
function post_request($url, $data, $referer='') { // Convert the data array into URL Parameters like a=b&foo=bar etc. $data = http_build_query($data); // parse the given URL $url = parse_url($url); if ($url['scheme'] != 'http') { die('Error: Only HTTP request are supported !'); } // extract host and path: $host = $url['host']; $path = $url['path']; // open a socket connection on port 80 - timeout: 30 sec $fp = fsockopen($host, 80, $errno, $errstr, 30); if ($fp){ // send the request headers: fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); if ($referer != '') fputs($fp, "Referer: $referer\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); $result = ''; while(!feof($fp)) { // receive the results of the request $result .= fgets($fp, 128); } } else { return array( 'status' => 'err', 'error' => "$errstr ($errno)" ); } // close the socket connection: fclose($fp); // split the result header from the content $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; // return as structured array: return array( 'status' => 'ok', 'header' => $header, 'content' => $content ); } //使用方法 // Submit those variables to the server $post_data = array( 'test' => 'foobar', 'okay' => 'yes', 'number' => 2 ); // Send a request to example.com $result = post_request('http://www.example.com/', $post_data); if ($result['status'] == 'ok'){ // Print headers echo $result['header']; echo '<hr />'; // print the result of the whole request: echo $result['content']; } else { echo 'A error occured: ' . $result['error']; }
希望本文所述對大家的php程序設(shè)計有所幫助。
您可能感興趣的文章:
- php 利用socket發(fā)送HTTP請求(GET,POST)
- PHP使用socket發(fā)送HTTP請求的方法
- 使用PHP Socket 編程模擬Http post和get請求
- php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼
- php基于socket實現(xiàn)SMTP發(fā)送郵件的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php socket方式提交的post詳解
- PHP中使用socket方式GET、POST數(shù)據(jù)實例
- PHP socket 模擬POST 請求實例代碼
- php自定義類fsocket模擬post或get請求的方法
- php使用socket調(diào)用http和smtp協(xié)議實例小結(jié)
相關(guān)文章
PHP ajax跨子域的解決方案之document.domain+iframe實例分析
這篇文章主要介紹了PHP ajax跨子域的解決方案之document.domain+iframe,結(jié)合實例形式分析了PHP ajax跨子域的解決方案document.domain+iframe的基本原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2020-03-03PHP使用Curl實現(xiàn)模擬登錄及抓取數(shù)據(jù)功能示例
這篇文章主要介紹了PHP使用Curl實現(xiàn)模擬登錄及抓取數(shù)據(jù)功能,結(jié)合實例形式分析了php使用curl進行登陸、驗證、cookie操作與數(shù)據(jù)抓取等相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-04-04php 數(shù)組操作(增加,刪除,查詢,排序)等函數(shù)說明
php 數(shù)組增加,刪除,查詢,排序詳細說明,需要的朋友可以參考下。2010-05-05php開發(fā)中的頁面跳轉(zhuǎn)方法總結(jié)
PHP頁面跳轉(zhuǎn)實現(xiàn)的功能就是將網(wǎng)站中一個網(wǎng)頁跳轉(zhuǎn)到另一個網(wǎng)頁中。對于剛剛學(xué)習(xí)PHP語言的朋友來說,是必須要掌握的基礎(chǔ)方法。2015-04-04