php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法
本文實(shí)例講述了php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法。分享給大家供大家參考。具體實(shí)現(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'];
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- php 利用socket發(fā)送HTTP請(qǐng)求(GET,POST)
- PHP使用socket發(fā)送HTTP請(qǐng)求的方法
- 使用PHP Socket 編程模擬Http post和get請(qǐng)求
- php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼
- php基于socket實(shí)現(xiàn)SMTP發(fā)送郵件的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php socket方式提交的post詳解
- PHP中使用socket方式GET、POST數(shù)據(jù)實(shí)例
- PHP socket 模擬POST 請(qǐng)求實(shí)例代碼
- php自定義類fsocket模擬post或get請(qǐng)求的方法
- php使用socket調(diào)用http和smtp協(xié)議實(shí)例小結(jié)
相關(guān)文章
用PHP將數(shù)據(jù)導(dǎo)入到Foxmail的實(shí)現(xiàn)代碼
下面的原理就是用PHP生成一個(gè)文件,然后下載并把這些資料導(dǎo)入他們的Foxmail地址簿中。2010-09-09
php保存二進(jìn)制原始數(shù)據(jù)為圖片的程序代碼
得到post過(guò)來(lái)的二進(jìn)制原始數(shù)據(jù),選擇一個(gè)生成路徑及圖片的名字,之后寫(xiě)入,思路很顯而易見(jiàn),需要的朋友可以收藏下2014-10-10
PHP實(shí)現(xiàn)基于mysqli的Model基類完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)基于mysqli的Model基類,給出了數(shù)據(jù)庫(kù)基類的完整實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2016-04-04
PHP三層結(jié)構(gòu)(上) 簡(jiǎn)單三層結(jié)構(gòu)
我們以一個(gè)簡(jiǎn)單的留言板代碼為例,先來(lái)看一個(gè)最簡(jiǎn)單的三層結(jié)構(gòu)代碼2010-07-07
PHP采用get獲取url漢字出現(xiàn)亂碼的解決方法
這篇文章主要介紹了PHP采用get獲取url漢字出現(xiàn)亂碼的解決方法,是很多PHP程序員都曾遇到的問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
php sprintf()函數(shù)讓你的sql操作更安全
本函數(shù)用來(lái)將字符串格式化。參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號(hào) % 開(kāi)始到轉(zhuǎn)換字符為止。而在轉(zhuǎn)換的格式間依序包括了2008-07-07

