Меню

Декабрь 2016

Как отправить XML или JSON документ по HTTP?

// sockets version HTTP/POST
function http_post( $url, $data ) {
	
	$eol = "\r\n";
	
	$post = '';

	if (is_array($data)) {
		foreach( $data as $k => $v)
			$post .= $k.'='.urlencode($v).'&';
		$post = substr($post,0,-1);
		$content_type = 'application/x-www-form-urlencoded';
	} else {
		$post = $data;
		if (strpos($post, ' 0 );
			
		} else if ($chunked) {
	
			while ( $chunk_length = hexdec(trim(fgets($fp))) ) {
				
				$chunk = '';
				$read_length = 0;

				while ( $read_length < $chunk_length ) {

					$chunk .= fread($fp, $chunk_length - $read_length);
					$read_length = strlen($chunk);

				}				
				$content .= $chunk;

				fgets($fp);

			}
		} else {
			while(!feof($fp)) $content .= fread($fp, 4096);
		}
		fclose($fp);
		
//		echo $content;
		
		return $content;
		
	} else {
		return false;
	}
}