php实现网页的post和获取post返回的信息


 <?php
/*远程处理类*/
class WebServer_data
{
	function WebServer_data()
	{
	}
	var $postdataArray;//提交的数组
	var $post_timeOut=30;
	var $public_Encryption_key='cntiadmin';//加密偏移量
	var $post_url="";  // 远程访问地址,不过现在不起作用
	function postdata($data=array())
	{
		if(count($data)>0)
		$this->postdataArray=$data;
	}

	/**
	 * 提交发送数据返回已序列化的PHP数据内容
	 *
	 * @param string $PostUrl 要指交的远程地址
	 * @param array $data 要提交的数据
     * @return obj  远程返回的对像
	 */
	function getWebServerData($PostUrl,$data)
	{
		$postReturnData=$this->post_data($PostUrl,$data);
		return unserialize($postReturnData);
	}

	/**
	 * 提交发送数据返回已序化的加密数据内容,回来还要解密
	 *
	 * @param unknown_type $PostUrl
	 * @param unknown_type $data
	 * @return unknown
	 */
	function getCrtyWebServerData($PostUrl,$data=array())
	{
		$postReturnData=$this->post_data($PostUrl,$data);
		if($postReturnData=="false") return "false";
		//return $postReturnData;
		//return $this->StrCode($postReturnData,$this->public_Encryption_key,'DECODE');
		return unserialize($this->StrCode($postReturnData,$this->public_Encryption_key,'DECODE'));
	}

	/**
	 * 字符串加解密
	 *
	 * $str=$Function_Class->strCode("abc","哈哈","ENCODE");
	 *$str=$Function_Class->strCode($str,"哈哈","DECODE");
	 * 
	 * 
	 * @param string $string  要处理的字符串
	 * @param string $offKey  加密的偏移量    为空则为拿当前用户请求的IP
	 * @param string $action  ENCODE加密    DECODE解密
	 * @return string
	 */
	function StrCode($string,$offKey="",$action='ENCODE'){
		if($offKey=="")
		$key	= substr(md5($_SERVER["HTTP_USER_AGENT"]),8,18);
		else
		$key=substr(md5($offKey),8,18);
		unset($offKey);
		$string	= $action == 'ENCODE' ? $string : base64_decode($string);
		$len	= strlen($key);
		$code	= '';
		for($i=0; $i<strlen($string); $i++){
			$k		= $i % $len;
			$code  .= $string[$i] ^ $key[$k];
		}
		$code = $action == 'DECODE' ? $code : base64_encode($code);
		return $code;
	}


	/**
     * 表单模拟提交发送
     *
     * @param string $PostUrl  要指交的远程地址
     * @param array $data    要提交的数据
     * @return string 
     */
	function post_data($PostUrl,$data=array())
	{
		if(count($data)>0)
		$this->postdataArray=$data;
		//检测输入
		$url_file = trim($PostUrl);
		if (empty($url_file)) { return false; }
		$url_arr = parse_url($url_file);
		if (!is_array($url_arr) || empty($url_arr)){ return false; }
		//获取请求数据
		$host = $url_arr['host'];
		if(!empty($url_arr['query'])&&$url_arr['query']!=null)
			$path = $url_arr['path'] ."?". $url_arr['query'];
		else 
			$path = $url_arr['path'];
		$port = isset($url_arr['port']) ? $url_arr['port'] : "80";
		//连接服务器
		$fp = fsockopen($host, $port, $err_no, $err_str, $this->post_timeOut);
		if (!$fp){ return false; }
		$ret="";
		$out = "";
		while (list ($k, $v) = each ($this->postdataArray)) {
			if(strlen($out) != 0) $out .= "&";
			$out .= rawurlencode($k). "=" .rawurlencode($v);
		}
		$out = trim ($out);
		$request     =   "POST ".$path."  HTTP/1.0\r\n";
		$request   .=   "Host: ".$host."\r\n";
		$request   .=   "User-Agent: Incutio HttpClient v0.9\r\n";
		$request   .=    "Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*\r\n";
		$request   .=   "Accept-encoding: gzip\r\n";
		$request   .=   "Accept-Language:   zh-cn\r\n";
		$request   .=   "Content-Type:   application/x-www-form-urlencoded\r\n";
		$request   .=   "Content-length:   ".strlen($out)."\r\n";
		$request   .=   "Connection:   Keep-Alive\r\n\r\n";
		$request.=$out;
		fputs($fp,$request);
		unset($request);
		$inHeaders = true;
		$atStart = true;
		while(!feof($fp)){
			$line = fgets($fp, 4096);
			if ($atStart) {
				// 是否第一次返回数据
				$atStart = false;
				if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
					return false;
				}
				continue;
			}
			if ($inHeaders) {
				if (trim($line) == '') {
					$inHeaders = false;
					continue;
				}
				if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
					continue;
				}
				continue;
			}
			$ret.= $line;
		}
		fclose ($fp);
		return $ret;
	}
	/**
	 * 设置提交数据
	 *
	 * @param obj $key
	 * @param obj $value
	 */
	function setPostData($key,$value)
	{
		$this->postdataArray[$key]=$value;
	}

	/**
	 * 函数:remote_file_exists
	*  功能:判断远程文件是否存在
	 *
	 * @param  string  $remoteFile  远程文件URL
	 * @return  bool 存在返回true,不存在或者其他原因返回false
	 */
	function remote_file_exists($remoteFile){
		//检测输入
		$url_file = trim($remoteFile);
		if (empty($url_file)) { return false; }
		$url_arr = parse_url($url_file);
		if (!is_array($url_arr) || empty($url_arr)){ return false; }

		//获取请求数据
		$host = $url_arr['host'];
		if(!empty($url_arr['query'])&&$url_arr['query']!=null)
			$path = $url_arr['path'] ."?". $url_arr['query'];
		else 
			$path = $url_arr['path'] ;
		$port = isset($url_arr['port']) ? $url_arr['port'] : "80";

		//连接服务器
		$fp = fsockopen($host, $port, $err_no, $err_str, 30);
		if (!$fp){ return false; }
		//构造请求协议
		$request_str = "GET ".$path." HTTP/1.1\r\n";
		$request_str .= "Host: ".$host."\r\n";
		$request_str .= "Connection: Close\r\n\r\n";
		//发送请求
		fwrite($fp, $request_str);
		$first_header = fgets($fp, 1024);
		fclose($fp);
		//判断文件是否存在
		if (trim($first_header) == ""){ return false; }
		if (!preg_match("/200/", $first_header)){
			return false;
		}
		return true;
	}
}
/*
require("Class_model/WebServer_data.php");
$postdate=new WebServer_data();
$postdate->setPostData("login","admin");
$postdate->setPostData("password","xiaosheng");
$arraystr=$postdate->post_data("http://192.168.1.130/soap_post.php");
如果返回是一个序列化的字符串,刚可以搞用下面这一句得到原来的值
$arraystr=unserialize($arraystr);
print_r($arraystr);
$obj=getWebServerData("http://192.168.1.130/soap_post.php");是返回请求地址返回的已序列化对
*/
?>

 

<?php
require_once(D_P_Client."Class_model/Webserver_data.php");
echo '开始';
$ws=new WebServer_data();
$data=array(
'username'=>'kenter1',
'type'=>'login',
);
echo '执行post';
print_r($ws->post_data("http://www.XXX.com/login.php",$data));
echo '完成';
?>

 

你可能感兴趣的:(PHP,xml,XHTML,SOAP,FP)