我试图向外部网站发送 POST 请求,到目前为止,由于同源策略,从 iv 读取的内容来看,这是不可能的。但我也读到代理可以绕过这一点。
如果我无法访问外部网站,这是否可能?我似乎无法澄清这一点。
我只想发送一个AJAX POST并获得响应,就像我使用Chrome的高级REST客户端一样。
使用
Access-Control-Allow-Origin: http://foo.example
对问题中的任何其他菜鸟
稍微修改了一下:https://github.com/eslachance/php-transparent-proxy
现在接受发布网址:
<?php
if(!function_exists('apache_request_headers')) {
// Function is from: http://www.electrictoolbox.com/php-get-headers-sent-from-browser/
function apache_request_headers() {
$headers = array();
foreach($_SERVER as $key => $value) {
if(substr($key, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
}
}
return $headers;
}
}
// Figure out requester's IP to shipt it to X-Forwarded-For
$ip = '';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
//echo "HTTP_CLIENT_IP: ".$ip;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
//echo "HTTP_X_FORWARDED_FOR: ".$ip;
} else {
$ip = $_SERVER['REMOTE_ADDR'];
//echo "REMOTE_ADDR: ".$ip;
}
//
preg_match('@^(?:http://)?([^/]+)@i', $_SERVER['HTTP_REFERER'], $matches);
$host = $matches[1];
preg_match('/[^.]+.[^.]+$/', $host, $matches);
$domainName = "{$matches[0]}";
//
//writelog($_POST);
$method = $_SERVER['REQUEST_METHOD'];
$desturl;
// parse the given URL
if($method == "POST") {
$desturl = $_POST['url'];
unset($_POST['url']);
}
else if ($method == "GET") {
$desturl = $_GET['url'];
unset($_GET['url']);
}
$response = proxy_request($desturl, ($method == "GET" ? $_GET : $_POST), $method);
$headerArray = explode("rn", $response[header]);
foreach($headerArray as $headerLine) {
header($headerLine);
}
echo $response[content];
function proxy_request($url, $data, $method) {
// Based on post_request from http://www.jonasjohn.de/snippets/php/post-request.htm
global $ip;
// Convert the data array into URL Parameters like a=b&foo=bar etc.
//$data = http_build_query($data);
$data = json_encode($data, JSON_FORCE_OBJECT);
writelog($data);
$datalength = strlen($data);
$url = parse_url($url);
//echo $url;
if ($url['scheme'] != 'http') {
die('Error: Only HTTP request are supported !');
}
// extract host and path:
$host = $url['host'];
$path = $url['path'];
// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp){
$out="";
if($method == "POST") {
$out ="POST $path HTTP/1.1rn";
} else {
$out ="GET $path?$data HTTP/1.1rn";
}
//Todo Get headers and forward them...
$requestHeaders = apache_request_headers();
while ((list($header, $value) = each($requestHeaders))) {
/*if($header !== "Connection" && $header !== "Host" && $header !== "Content-Length"
&& $header !== "Content-Type"
&& $header !== "Origin"
&& $header !== "Referer"
&& $header !== "X-Requested-With"
&& $header !== "Accept-Encoding"
) {
// $out.= "$header: $valuern";
writelog("$header: $valuern");
}*/
if($header == "Cookie" && $header == "User-Agent" ) {
$out.= "$header: $valuern";
//writelog("$header: $valuern");
}
}
$out.= "Host: $hostrn";
$out.= "Content-Type: application/json; charset=UTF-8rn";
$out.= "Content-Length: $datalengthrn";
$out.= "Connection: Closernrn";
$out.= $data;
fwrite($fp, $out);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
}
else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
// close the socket connection:
fclose($fp);
// split the result header from the content
$result = explode("rnrn", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
// return as structured array:
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
);
}
?>