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將數(shù)據(jù)導(dǎo)入到Foxmail的實現(xiàn)代碼
下面的原理就是用PHP生成一個文件,然后下載并把這些資料導(dǎo)入他們的Foxmail地址簿中。2010-09-09
PHP實現(xiàn)基于mysqli的Model基類完整實例
這篇文章主要介紹了PHP實現(xiàn)基于mysqli的Model基類,給出了數(shù)據(jù)庫基類的完整實現(xiàn)與使用方法,需要的朋友可以參考下2016-04-04
PHP三層結(jié)構(gòu)(上) 簡單三層結(jié)構(gòu)
我們以一個簡單的留言板代碼為例,先來看一個最簡單的三層結(jié)構(gòu)代碼2010-07-07
PHP采用get獲取url漢字出現(xiàn)亂碼的解決方法
這篇文章主要介紹了PHP采用get獲取url漢字出現(xiàn)亂碼的解決方法,是很多PHP程序員都曾遇到的問題,非常具有實用價值,需要的朋友可以參考下2014-11-11
php sprintf()函數(shù)讓你的sql操作更安全
本函數(shù)用來將字符串格式化。參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號 % 開始到轉(zhuǎn)換字符為止。而在轉(zhuǎn)換的格式間依序包括了2008-07-07

