PHP中发送post请求,不带附件

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.0\r\n";
			$header .= "Accept: */*\r\n";
			$header .= "Accept-Language: zh-cn\r\n";
			$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
			$header .= "User-Agent: 
{1}

SERVER[HTTP_USER_AGENT]\r\n";$header .= "Host: $host\r\n";$header .= 'Content-Length: ' . $length . "\r\n";$header .= "Connection: Close\r\n";$header .= "Cache-Control: no-cache\r\n";$header .= "Cookie: $cookie\r\n\r\n";$header .= $params . "\r\n";} else {$header = "GET $path HTTP/1.0\r\n";$header .= "Accept: */*\r\n";$header .= "Accept-Language: zh-cn\r\n";$header .= "User-Agent:
{1}

SERVER[HTTP_USER_AGENT]\r\n";$header .= "Host: $host\r\n";$header .= "Connection: Close\r\n";$header .= "Cookie: $cookie\r\n\r\n";}$fp = @fsockopen ( ($ip ? $ip : $host), $port, $errno, $errstr, $timeout );if (! $fp) {return ''; //note $errstr : $errno \r\n} 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 == "\r\n" || $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;}}}


 

你可能感兴趣的:(PHP,Stream,header,query,Path,FP)