Home >Backend Development >PHP Tutorial >应用php模拟http请求

应用php模拟http请求

WBOY
WBOYOriginal
2016-06-13 10:40:39812browse

使用php模拟http请求

// Returns array with headers in $response[0] and body in $response[1]function http_post($request, $host, $path, $port = 80, $ip=null) {			$http_request  = "POST $path HTTP/1.0\r\n";		$http_request .= "Host: $host\r\n";		$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";		$http_request .= "Content-Length: {$content_length}\r\n";		$http_request .= "User-Agent: {$akismet_ua}\r\n";		$http_request .= "\r\n";		$http_request .= $request;				$response = '';		if( false != ( $fs = @fsockopen( $http_host, $port, $errno, $errstr, 10 ) ) ) {			fwrite( $fs, $http_request );			while ( !feof( $fs ) )				$response .= fgets( $fs, 1160 ); // One TCP-IP packet			fclose( $fs );			$response = explode( "\r\n\r\n", $response, 2 );		}		return $response;	}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn