关于php搭建http代理的信息

http://www.itjxue.com  2023-01-18 03:19  来源:未知  点击次数: 

怎么用PHP发送HTTP请求

方法一:利用php的socket编程来直接给接口发送数据来模拟post的操作。

建立两个文件post.php,getpost.php

post.php内容如下:

?php

$flag = 0;

$post = '';

$errno = '';

$errstr = '';

//要post的数据

$argv = array(

'var1'='abc',

'var2'='how are you , my friend??');

//构造要post的字符串

foreach ($argv as $key=$value) {

if ($flag!=0) {

$post .= "";

$flag = 1;

}

$post.= $key."="; $post.= urlencode($value);

$flag = 1;

}

$length = strlen($post);

//创建socket连接

$fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."---".$errno);

//构造post请求的头

$header = "POST /flandy/getpost.php HTTP/1.1\r\n";

$header .= "Host:127.0.0.1\r\n";

$header .= "Referer:/flandy/post.php\r\n";

$header .= "Content-Type: application/x-www-form-urlencoded\r\n";

$header .= "Content-Length: ".$length."\r\n";

$header .= "Connection: Close\r\n\r\n";

//添加post的字符串

$header .= $post."\r\n";

//发送post的数据

fputs($fp,$header);

$inheader = 1;

while (!feof($fp)) {

$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据

if ($inheader ($line == "\n" || $line == "\r\n")) {

$inheader = 0;

}

if ($inheader == 0) {

echo $line;

}

}

fclose($fp);

?

getpost.php的内容如下

?php

echo "this is the data posted";

echo "pre";

print_r($_REQUEST);

echo "/pre";

?

结果输出:

this is the data posted

Array

(

[var1] = abc

[var2] = how are you , my friend??

)

以上代码在本机81端口下已经通过测试。

方法二:

使用PHP的curl扩展或HttpClient.class.php类,这两个非常类似,下面简单的列出curl的实现代码。

两个文件post2.php和getpost2.php

post2.php的内容如下:

?php

$psecode = ’NDE005’;

$website = ’’;

$amt = 1;

$pwd = 123456;

$ch = curl_init();

$curl_url = "" . $website .

"pwd=" . $pwd . "action=checkpseid=" . $psecode .

"amt=" . $amt;

curl_setopt($ch, CURLOPT_URL, $curl_url);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量

$curl_result = curl_exec($ch);

$result = explode(',', $curl_result);

curl_close($ch);

print_r($result);

?

getpost2.php的内容如下:

?php

echo "returndatabr";

echo "pre";

print_r($_REQUEST);

echo "/pre";

?

结果输出:

Array ( [0] = returndataArray

(

[web] = ’wwwbaiducom’

[pwd] = 123456

[action] = check

[pseid] = ’NDE005’

[amt] = 1

)

)

phpstorm 怎么设置http代理服务器

1:点击File-Settings-Deployment 。点+按钮增加服务

2:填写Apache服务器的路径

3:现在打开通过PhpStorm打开浏览器再试试就可以了。

PHP使用代理方法获取远程网页的代码.

不要用file_get_contents函数。

用snoopy的类,网上有snoopy.class.php,你自行百度查找。

snoopy的类可以设置$proxy_host参数,设置代理主机,$proxy_port是代理主机端口。你下载一个下来,网上的教程很多,看看应该明白。

可以通过购买虚拟主机搭建自己的http代理服务器吗?

虚拟主机是无法作为VPN来使用的 建议您使用香港VPS或者云主机 , 可以开启VPN服务。 希望可以帮到你 时代互联为您解答

使用thinkphp 怎么实现反向代理

改自PHP Reverse Proxy PRP,修改了原版中的一些错误,支持了文件上传以及上传文件类型识别,支持指定IP,自适应SAE环境。

使用方法

?123456789 ?php $proxy=new PhpReverseProxy(); $proxy-port="8080"; $proxy-host="ww"; //$proxy-ip="1.1.1.1"; $proxy-forward_path=""; $proxy-connect(); $proxy-output(); ?

源代码

?php //Source Code: http //www xiumu.org/technology/php-reverse-proxy-class.shtml class PhpReverseProxy{ public $publicBaseURL; public $outsideHeaders; public $XRequestedWith; public $sendPost; public $port,$host,$ip,$content,$forward_path,$content_type,$user_agent, $XFF,$request_method,$IMS,$cacheTime,$cookie,$authorization; private $http_code,$lastModified,$version,$resultHeader; const chunkSize = 10000; function __construct(){ $this-version="PHP Reverse Proxy (PRP) 1.0"; $this-port="8080"; $this-host="127.0.0.1"; $this-ip=""; $this-content=""; $this-forward_path=""; $this-path=""; $this-content_type=""; $this-user_agent=""; $this-http_code=""; $this-XFF=""; $this-request_method="GET"; $this-IMS=false; $this-cacheTime=72000; $this-lastModified=gmdate("D, d M Y H:i:s",time()-72000)." GMT"; $this-cookie=""; $this-XRequestedWith = ""; $this-authorization = ""; } function translateURL($serverName) { $this-path=$this-forward_path.$_SERVER['REQUEST_URI']; if(IS_SAE) return $this-translateServer($serverName).$this-path; if($_SERVER['QUERY_STRING']=="") return $this-translateServer($serverName).$this-path; else return $this-translateServer($serverName).$this-path."?".$_SERVER['QUERY_STRING']; } function translateServer($serverName) { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = $this-left(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; if($this-port=="") return $protocol."://".$serverName; else return $protocol."://".$serverName.":".$this-port; } function left($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); } function preConnect(){ $this-user_agent=$_SERVER['HTTP_USER_AGENT']; $this-request_method=$_SERVER['REQUEST_METHOD']; $tempCookie=""; foreach ($_COOKIE as $i = $value) { $tempCookie=$tempCookie." $i=$_COOKIE[$i];"; } $this-cookie=$tempCookie; if(empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ $this-XFF=$_SERVER['REMOTE_ADDR']; } else { $this-XFF=$_SERVER['HTTP_X_FORWARDED_FOR'].", ".$_SERVER['REMOTE_ADDR']; } } function connect(){ if(empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ $this-preConnect(); $ch=curl_init(); if($this-request_method=="POST"){ curl_setopt($ch, CURLOPT_POST,1); $postData = array(); $filePost = false; $uploadPath = 'uploads/'; if (IS_SAE) $uploadPath = SAE_TMP_PATH; if(count($_FILES)0){ if(!is_writable($uploadPath)){ die('You cannot upload to the specified directory, please CHMOD it to 777.'); } foreach($_FILES as $key = $fileArray){ copy($fileArray["tmp_name"], $uploadPath . $fileArray["name"]); $proxyLocation = "@" . $uploadPath . $fileArray["name"] . ";type=" . $fileArray["type"]; $postData = array($key = $proxyLocation); $filePost = true; } } foreach($_POST as $key = $value){ if(!is_array($value)){ $postData[$key] = $value; } else{ $postData[$key] = serialize($value); } } if(!$filePost){ //$postData = http_build_query($postData); $postString = ""; $firstLoop = true; foreach($postData as $key = $value){ $parameterItem = urlencode($key)."=".urlencode($value); if($firstLoop){ $postString .= $parameterItem; } else{ $postString .= "".$parameterItem; } $firstLoop = false; } $postData = $postString; } //echo print_r($postData); //curl_setopt($ch, CURLOPT_VERBOSE, 0); //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); $this-sendPost = $postData; //var_dump(file_exists(str_replace('@','',$postData['imgfile'])));exit; curl_setopt($ch, CURLOPT_POSTFIELDS,$postData); //curl_setopt($ch, CURLOPT_POSTFIELDS,file_get_contents($proxyLocation)); //curl_setopt($ch, CURLOPT_POSTFIELDS,file_get_contents("php://input")); } //gets rid of mulitple ? in URL $translateURL = $this-translateURL(($this-ip)?$this-ip:$this-host); if(substr_count($translateURL, "?")1){ $firstPos = strpos($translateURL, "?", 0); $secondPos = strpos($translateURL, "?", $firstPos + 1); $translateURL = substr($translateURL, 0, $secondPos); } curl_setopt($ch,CURLOPT_URL,$translateURL); $proxyHeaders = array( "X-Forwarded-For: ".$this-XFF, "User-Agent: ".$this-user_agent, "Host: ".$this-host ); if(strlen($this-XRequestedWith)1){ $proxyHeaders[] = "X-Requested-With: ".$this-XRequestedWith; //echo print_r($proxyHeaders); } curl_setopt($ch,CURLOPT_HTTPHEADER, $proxyHeaders); if($this-cookie!=""){ curl_setopt($ch,CURLOPT_COOKIE,$this-cookie); } curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false); curl_setopt($ch,CURLOPT_AUTOREFERER,true); curl_setopt($ch,CURLOPT_HEADER,true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $output=curl_exec($ch); $info = curl_getinfo( $ch ); curl_close($ch); $this-postConnect($info,$output); }else { $this-lastModified=$_SERVER['HTTP_IF_MODIFIED_SINCE']; $this-IMS=true; } } function postConnect($info,$output){ $this-content_type=$info["content_type"]; $this-http_code=$info['http_code']; //var_dump($info);exit; if(!empty($info['last_modified'])){ $this-lastModified=$info['last_modified']; } $this-resultHeader=substr($output,0,$info['header_size']); $content = substr($output,$info['header_size']); if($this-http_code=='200'){ $this-content=$content; }elseif( ($this-http_code=='302' || $this-http_code=='301') isset($info['redirect_url'])){ $redirect_url = str_replace($this-host,$_SERVER['HTTP_HOST'],$info['redirect_url']); if (IS_SAE) $redirect_url = str_replace('','',$info['redirect_url']); header("Location: $redirect_url"); exit; }elseif($this-http_code=='404'){ header("HTTP/1.1 404 Not Found"); exit("HTTP/1.1 404 Not Found"); }elseif($this-http_code=='500'){ header('HTTP/1.1 500 Internal Server Error'); exit("HTTP/1.1 500 Internal Server Error"); }else{ exit("HTTP/1.1 ".$this-http_code." Internal Server Error"); } } function output(){ $currentTimeString=gmdate("D, d M Y H:i:s",time()); $expiredTime=gmdate("D, d M Y H:i:s",(time()+$this-cacheTime)); $doOriginalHeaders = true; if($doOriginalHeaders){ if($this-IMS){ header("HTTP/1.1 304 Not Modified"); header("Date: Wed, $currentTimeString GMT"); header("Last-Modified: $this-lastModified"); header("Server: $this-version"); }else{ header("HTTP/1.1 200 OK"); header("Date: Wed, $currentTimeString GMT"); header("Content-Type: ".$this-content_type); header("Last-Modified: $this-lastModified"); header("Cache-Control: max-age=$this-cacheTime"); header("Expires: $expiredTime GMT"); header("Server: $this-version"); preg_match("/Set-Cookie:[^\n]*/i",$this-resultHeader,$result); foreach($result as $i=$value){ header($result[$i]); } preg_match("/Content-Encoding:[^\n]*/i",$this-resultHeader,$result); foreach($result as $i=$value){ //header($result[$i]); } preg_match("/Transfer-Encoding:[^\n]*/i",$this-resultHeader,$result); foreach($result as $i=$value){ //header($result[$i]); } echo($this-content); /* if(stristr($this-content, "error")){ echo print_r($this-sendPost); } */ } } else{ $headerString = $this-resultHeader; //string $headerArray = explode("\n", $headerString); foreach($headerArray as $privHeader){ header($privHeader); } if(stristr($headerString, "Transfer-encoding: chunked")){ flush(); ob_flush(); $i = 0; $maxLen = strlen($this-content); while($i $maxLen){ $endChar = $i + self::chunkSize; if($endChar = $maxLen){ $endChar = $maxLen - 1; } $chunk = substr($this-content, $i, $endChar); $this-dump_chunk($chunk); flush(); ob_flush(); $i = $i + $endChar; } } else{ echo($this-content); } //echo "header: ".print_r($headerArray); //header($this-resultHeader); } } function dump_chunk($chunk) { echo sprintf("%x\r\n", strlen($chunk)); echo $chunk; echo "\r\n"; } function getOutsideHeaders(){ $headers = array(); foreach ($_SERVER as $name = $value){ if (substr($name, 0, 5) == 'HTTP_') { $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); $headers[$name] = $value; }elseif ($name == "CONTENT_TYPE") { $headers["Content-Type"] = $value; }elseif ($name == "CONTENT_LENGTH") { $headers["Content-Length"] = $value; }elseif(stristr($name, "X-Requested-With")) { $headers["X-Requested-With"] = $value; $this-XRequestedWith = $value; } } //echo print_r($headers); $this-outsideHeaders = $headers; return $headers; } } ?

(责任编辑:IT教学网)

更多

推荐微信营销文章