先看看浏览器的头信息,谷歌浏览器的头信息大概是这样
消息头
Request URL:http://cs/txtp/index.php?route=checkout/cart/add Request Method:POST Status Code:200 OK Remote Address:127.0.0.1:80
响应头 Response Headers
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Content-Length:52 Content-Type:application/json Date:Fri, 11 Nov 2016 04:44:45 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Pragma:no-cache Server:Microsoft-IIS/10.0 Set-Cookie:default=94137517446a643182e1c5beb3; path=/; httponly X-Powered-By:PHP/5.4.45 X-Powered-By:ASP.NET
请求头 Request Headers
Accept:application/json, text/javascript, */*; q=0.01 Accept-Encoding:gzip, deflate Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4 Connection:keep-alive Content-Length:64 Content-Type:application/x-www-form-urlencoded; charset=UTF-8 Cookie:cookieAcceptanceCookie=accepted; olfsk=olfsk04720746790230024; hblid=c0s2j17AWtDrbtot493Lw9X1GH3EM0R2; currency=USD; PHPSESSID=u24nck223cd86pqtai0stc08e6; language=zh-HK; __atuvc=83%7C41%2C3%7C42%2C2%7C43%2C49%7C44%2C11%7C45; default=94137517446a643182e1c5beb3 Host:cs Origin:http://cs Referer:http://cs/txtp/index.php?route=product/product&product_id=76 User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36 X-Requested-With:XMLHttpRequest
查询字符串参数 Query String Parameters
route:checkout/cart/add
参数 Form Data
option[298]:117 option[299]:121 quantity:1 product_id:76
火狐的头信息大概是这样的
Host: cs User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Cookie: language=zh-HK; hblid=AUtt7xnfu4LKjEQG493Lw1X1GH0VoOAT; olfsk=olfsk4114304963449056; __atuvc=6%7C41%2C0%7C42%2C0%7C43%2C0%7C44%2C1%7C45; currency=USD; PHPSESSID=oljr4n8grvk170m8pfl7r0sen2; default=8cc444c3ab775a1ac3ea5e39fe Connection: keep-alive
一,请求方设置自己的头信息,header.php
<?php function FormatHeader($url, $myIp = null,$xml = null) { // 解析url $temp = parse_url($url); $query = isset($temp['query']) ? $temp['query'] : ''; $path = isset($temp['path']) ? $temp['path'] : '/'; $header = array ( "POST {$path}?{$query} HTTP/1.1", "Host: {$temp['host']}", "Content-Type: text/xml; charset=utf-8", 'Accept: */*', "Referer: http://{$temp['host']}/", 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)', "X-Forwarded-For: {$myIp}", "Content-length: 380", "Connection: Close" ); return $header; } $interface = 'http://localhost/test/header2.php'; $header = FormatHeader($interface,'10.1.11.1'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $interface); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方 curl_setopt($ch, CURLOPT_HEADER, 0); //不取得返回头信息 curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); var_dump($result); ?>
二,被请求方,取得头信息,header2.php
<?php print_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的 ?>
三,看一下header.php请求的结果
string(1045) "Array ( [HTTP_HOST] => localhost [CONTENT_TYPE] => text/xml; charset=utf-8 [HTTP_ACCEPT] => */* [HTTP_REFERER] => http://localhost/ [HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1) [HTTP_X_FORWARDED_FOR] => 10.1.11.1 [CONTENT_LENGTH] => 380 [PATH] => /usr/local/bin:/usr/bin:/bin [SERVER_SIGNATURE] => <address>Apache/2.2.16 (Ubuntu) Server at localhost Port 80</address> 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 )
上面那几个,我们可以明显看到,是我设置的头信息。
四,取得返回的头信息
curl_setopt($ch, CURLOPT_HEADER, 1); //取得返回头信息
我们把CURLOPT_HEADER设置成1,在取得的结果当中,显示数组的前面会有这些信息
string(1239) "HTTP/1.1 200 OK Date: Fri, 27 May 2011 01:57:57 GMT Server: Apache/2.2.16 (Ubuntu) X-Powered-By: PHP/5.3.3-1ubuntu9.5 Vary: Accept-Encoding Content-Length: 1045 Content-Type: text/html Array ( [HTTP_HOST] => localhost [CONTENT_TYPE] => text/xml; charset=utf-8 [HTTP_ACCEPT] => */*
五,$_SERVER部分头信息是拿不到的
修改一下header.php
<?php function FormatHeader($url, $myIp = null,$xml = null) { // 解悉url $temp = parse_url($url); $query = isset($temp['query']) ? $temp['query'] : ''; $path = isset($temp['path']) ? $temp['path'] : '/'; $header = array ( "POST {$path}?{$query} HTTP/1.1", "Host: {$temp['host']}", "Content-Type: text/xml; charset=utf-8", 'Accept: */*', "Referer: http://{$temp['host']}/", 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)', "X-Forwarded-For: {$myIp}", "Content-length: " . strlen($xml) ."\r\n\r\n" .$xml, //修改1 "Connection: Close" ); return $header; } $xml = '<?xml version="1.0" encoding="utf-8"?> //修改2 <profile> <sha1>adsfadsf</sha1> <user_id>asdfasdf</user_id> <album_id>asdf</album_id> <album_name>asdf</album_name> <tags>asdfasd</tags> <title>asdfasdf</title> <content>asdfadsf</content> <type>asdfasdf</type> <copyright>asdfasdf</copyright> </profile>'; $interface = 'http://localhost/test/header2.php'; $header = FormatHeader($interface,'10.1.11.1',$xml); //修改3 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $interface); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方 curl_setopt($ch, CURLOPT_HEADER, 0); //不取得返回头信息 curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); var_dump($result); ?>
如果这样的话,header2.php里面,打印$_SERVER不可能把头信息中的xml打印出来。这个时候,我们在header2.php后面加上以下二行
$raw_post_data = file_get_contents('php://input', 'r'); var_dump($raw_post_data);
这样就可以取到$xml的内容,并且只会取$xml的内容。
php获取客户端真实ip地址的三种方法
第一种方法
function get_real_ip(){ $ip=false; if(!empty($_SERVER['HTTP_CLIENT_IP'])){ $ip=$_SERVER['HTTP_CLIENT_IP']; } if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']); if($ip){ array_unshift($ips, $ip); $ip=FALSE; } for ($i=0; $i < count($ips); $i++){ if(!eregi ('^(10│172.16│192.168).', $ips[$i])){ $ip=$ips[$i]; break; } } } return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); }
第二种方法:
function get_real_ip(){ static $realip; if(isset($_SERVER)){ if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $realip=$_SERVER['HTTP_X_FORWARDED_FOR']; }else if(isset($_SERVER['HTTP_CLIENT_IP'])){ $realip=$_SERVER['HTTP_CLIENT_IP']; }else{ $realip=$_SERVER['REMOTE_ADDR']; } }else{ if(getenv('HTTP_X_FORWARDED_FOR')){ $realip=getenv('HTTP_X_FORWARDED_FOR'); }else if(getenv('HTTP_CLIENT_IP')){ $realip=getenv('HTTP_CLIENT_IP'); }else{ $realip=getenv('REMOTE_ADDR'); } } return $realip; }
第三种方法,摘自DISCUZ,应该还不错吧!
// 获取IP地址(摘自discuz) function getIp(){ $ip='未知IP'; if(!empty($_SERVER['HTTP_CLIENT_IP'])){ return is_ip($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:$ip; }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ return is_ip($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$ip; }else{ return is_ip($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:$ip; } } function is_ip($str){ $ip=explode('.',$str); for($i=0;$i<count($ip);$i++){ if($ip[$i]>255){ return false; } } return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',$str); }
php http 协议 发送post请求 中文字符长度怎么算
如我有个post 数据 $date = 'name=adasd&ccc=aaa&bbb=ddd' http协议 发Content-length: 可以先strlen($date);
先将POST数据转化为字符串
以下函数主要原理还是应用递归方式,将多维数组转化为一维数组进行,最后进行数组转字符串处理既可拿到POST过来的数据字符串化;
核心代码:
function getPostLog(array $_data = array(),$n = ''){ if(empty($_data)) return ''; $_gPOST = $_data; $_rs = array(); foreach ($_gPOST AS $name=>$value){ if( is_array($value) ){ $_rs[] = getPostLog($value,$name); }else{ if( !empty($_data) ){ $_rs[] = $n.''.$name.''.'='.$value; }else{ $_rs[] = $name.'='.$value; } } } $_rs = implode('&', $_rs); return $_rs; }
然后在URL编码
$strlen = strlen(str_replace(array('%3D','%26'),array('=','&'),urlencode(getPostLog($data))))
大功告成