php发送post请求无附件的例子

发布时间:2019-12-18编辑:脚本学堂
有关php发送post请求的一段代码,php使用post方式传输数据的例子,此段代码不带附件。

php发送post请求
 

复制代码 代码示例:
if (! function_exists ( "send_post" )) { 
function send_post($url, $post = '', $cookie = '') { 
$limit = 0; 
$bysocket = FALSE; 
$ip = ''; 
$timeout = 15; 
$block = TRUE; 
 
$return = ''; 
$matches = parse_url ( $url ); 
! isset ( $matches ['host'] ) && $matches ['host'] = ''; 
! isset ( $matches ['path'] ) && $matches ['path'] = ''; 
! isset ( $matches ['query'] ) && $matches ['query'] = ''; 
! isset ( $matches ['port'] ) && $matches ['port'] = ''; 
$host = $matches ['host']; 
$path = $matches ['path'] ? $matches ['path'] . ($matches ['query'] ? '?' . $matches ['query'] : '') : '/'; 
$port = ! empty ( $matches ['port'] ) ? $matches ['port'] : 80; 
 
if ($post) { 
$params = ""; 
$flag = 0; 
foreach ( $post as $key => $value ) { 
if ($flag != 0) { 
$params .= "& "; 
$flag = 1; 

$params .= $key . "="; 
$params .= urlencode ( $value ); 
$flag = 1; 

$length = strlen ( $params ); 
 
$header = "POST $path HTTP/1.0rn"; 
$header .= "Accept: */*rn"; 
$header .= "Accept-Language: zh-cnrn"; 
$header .= "Content-Type: application/x-www-form-urlencodedrn"; 
$header .= "User-Agent: {1}
SERVER[HTTP_USER_AGENT]rn";$header .= "Host: $hostrn";$header .= 'Content-Length: ' . $length . "rn";$header .= "Connection: Closern";$header .= "Cache-Control: no-cachern";$header .= "Cookie: $cookiernrn";$header .= $params . "rn";} else {$header 
 = "GET $path HTTP/1.0rn";$header .= "Accept: */*rn";$header .= "Accept-Language: zh-cnrn";$header .= "User-Agent: {1}
SERVER[HTTP_USER_AGENT]rn";$header .= "Host: $hostrn";$header .= "Connection: Closern";$header .= "Cookie: $cookiernrn";}$fp = @fsockopen ( ($ip ? $ip : $host), $port, $errno, $errstr, $timeout );if (! $fp) {return ''; //note $errstr : $errno rn} 
 else {stream_set_blocking ( $fp, $block );stream_set_timeout ( $fp, $timeout );@fwrite ( $fp, $header );$status = stream_get_meta_data ( $fp );if (! $status ['timed_out']) {while ( ! feof ( $fp ) ) {if (($header = @fgets ( $fp )) && ($header == "rn" || $header 
 == "n")) {break;}}$stop = false;while ( ! feof ( $fp ) && ! $stop ) {$data = fread ( $fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit) );$return .= $data;if ($limit) {$limit -= strlen ( $data );$stop = $limit <= 0;}}}@fclose ( $fp );return $return;}}}