牛骨文教育服务平台(让学习变的简单)
博文笔记

CURL 修改增进版,可以设置用户名和密码 2013-1-30

创建时间:2013-01-30 投稿人: 浏览次数:6109

修改的地方:

(1)可以设置请求需要提供用户名密码的网址。

(2)CURL请求和SOCKET请求都加了是否需要密码的判断。

如果发现错误或者问题,可以随时联系我 admin@zbphp.com,共同探讨。



<?php

/**
 *      [ZBPHP.COM] (C)2001-2099
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: function_filesock.php 2013-01-30 23:44:15Z admin@zbphp.com $
 */



function _dfsockopen($url, $limit = 0, $post = "", $cookie = "", $bysocket = FALSE, $ip = "", $timeout = 15, $block = TRUE, $encodetype  = "URLENCODE", $allowcurl = TRUE, $position = 0) {
	$return = "";
	$matches = parse_url($url);
	$scheme = $matches["scheme"];
	$host = $matches["host"];
	$path = $matches["path"] ? $matches["path"].($matches["query"] ? "?".$matches["query"] : "") : "/";
	$port = !empty($matches["port"]) ? $matches["port"] : 80;


	if(function_exists("curl_init") && function_exists("curl_exec") && $allowcurl) {
	    $chURL = $scheme."://".($matches["user"] ? $matches["user"].":".$matches["pass"]."@":"").($ip ? $ip : $host).":".$port.$path;
		$ch = curl_init();
		$ip && curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: ".$host));
		if($matches["user"] || $matches["pass"])
		{
            curl_setopt($curl, CURLOPT_USERPWD, $matches["user"].":".$matches["pass"]);
            curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        }
		curl_setopt($ch, CURLOPT_URL, $chURL);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		if($post) {
			curl_setopt($ch, CURLOPT_POST, 1);
			if($encodetype == "URLENCODE") {
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
			} else {
				parse_str($post, $postarray);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray);
			}
		}
		if($cookie) {
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		$data = curl_exec($ch);
		$status = curl_getinfo($ch);
		$errno = curl_errno($ch);
		curl_close($ch);
		if($errno || $status["http_code"] != 200) {
			return;
		} else {
			return !$limit ? $data : substr($data, 0, $limit);
		}
	}

	if($post) {
		$out = "POST $path HTTP/1.0
";
		$header = "Accept: */*
";
		$header .= "Accept-Language: zh-cn
";
		$boundary = $encodetype == "URLENCODE" ? "" : "; boundary=".trim(substr(trim($post), 2, strpos(trim($post), "
") - 2));
		$header .= $encodetype == "URLENCODE" ? "Content-Type: application/x-www-form-urlencoded
" : "Content-Type: multipart/form-data$boundary
";
		$header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]
";
		$header .= "Host: $host:$port
";
		if(!empty($matches["user"]))
		{
		    $header .= "Authorization: Basic ".base64_encode($matches["user"].":".$matches["pass"])."
";
		}
		$header .= "Content-Length: ".strlen($post)."
";
		$header .= "Connection: Close
";
		$header .= "Cache-Control: no-cache
";
		$header .= "Cookie: $cookie

";
		$out .= $header.$post;
	} else {
		$out = "GET $path HTTP/1.0
";
		$header = "Accept: */*
";
		$header .= "Accept-Language: zh-cn
";
		$header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]
";
		$header .= "Host: $host:$port
";
		if(!empty($matches["user"]))
		{
		    $header .= "Authorization: Basic ".base64_encode($matches["user"].":".$matches["pass"])."
";
		}
		$header .= "Connection: Close
";
		$header .= "Cookie: $cookie

";
		$out .= $header;
	}

	$fpflag = 0;
	if(!$fp = @fsocketopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout)) {
		$context = array(
			"http" => array(
				"method" => $post ? "POST" : "GET",
				"header" => $header,
				"content" => $post,
				"timeout" => $timeout,
			),
		);
		$context = stream_context_create($context);
		$fp = @fopen($scheme."://".($ip ? $ip : $host).":".$port.$path, "b", false, $context);
		$fpflag = 1;
	}

	if(!$fp) {
		return "";
	} else {
		stream_set_blocking($fp, $block);
		stream_set_timeout($fp, $timeout);
		@fwrite($fp, $out);
		$status = stream_get_meta_data($fp);
		if(!$status["timed_out"]) {
			while (!feof($fp) && !$fpflag) {
				if(($header = @fgets($fp)) && ($header == "
" ||  $header == "
")) {
					break;
				}
			}

			if($position) {
				for($i=0; $i<$position; $i++) {
					$char = fgetc($fp);
					if($char == "
" && $oldchar != "
") {
						$i++;
					}
					$oldchar = $char;
				}
			}

			if($limit) {
				$return = stream_get_contents($fp, $limit);
			} else {
				$return = stream_get_contents($fp);
			}
		}
		@fclose($fp);
		return $return;
	}
}

?>




声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。