也许你能比我更好地理解它。我很擅长 php,但不太擅长发送不同的标头和内容。它的工作原理是您将 ajax 帖子发布到代理文件。代理文件位于外部服务器上
$.ajax({
type: "POST",
url: "http://mywebsite.com/ajax-proxy.php",
data: 'csurl=www.google.com',
error: function(e) {console.log(e);},
success: function(msg){console.log(msg)}
});
您还必须传递 csurl,它是代理将您转发到的 url。在这个例子中,我使用了谷歌。但我通常用作csurl的是我将存储ajax数据的目录
在代理文件中,有一个
$valid_requests = array()
在该数组中,您声明了您希望代理批准的所有 url。在此示例中,您输入 www.google.com(注意:必须与 csurl 参数完全相同,否则将不起作用)
下面是代理文件
<?php
/**
* AJAX Cross Domain (PHP) Proxy 0.6
* by Iacovos Constantinou (http://www.iacons.net)
*
* Released under CC-GNU GPL
*/
/**
* Enables or disables filtering for cross domain requests.
* Recommended value: true, for security reasons
*/
define('CSAJAX_FILTERS', true);
/**
* A set of valid cross domain requests
*/
$valid_requests = array(
'www.google.com'
);
/*** STOP EDITING HERE UNLESS YOU KNOW WHAT YOU ARE DOING ***/
// identify request headers
$request_headers = array();
foreach ( $_SERVER as $key=>$value ) {
if( substr($key, 0, 5) == 'HTTP_' ) {
$headername = str_replace('_', ' ', substr($key, 5));
$headername = str_replace(' ', '-', ucwords(strtolower($headername)));
$request_headers[$headername] = $value;
}
}
// identify request method, url and params
$request_method = $_SERVER['REQUEST_METHOD'];
$request_params = ( $request_method == 'GET' ) ? $_GET : $_POST;
$request_url = urldecode($request_params['csurl']);
$p_request_url = parse_url($request_url);
unset($request_params['csurl']);
// ignore requests for proxy :)
if ( preg_match('!'. $_SERVER['SCRIPT_NAME'] .'!', $request_url) || empty($request_url) ) {
exit;
}
// check against valid requests
if ( CSAJAX_FILTERS ) {
$parsed = $p_request_url;
$check_url = isset($parsed['scheme']) ? $parsed['scheme'] .'://' : '';
$check_url .= isset($parsed['user']) ? $parsed['user'] . ($parsed['pass'] ? ':'. $parsed['pass']:'') .'@' : '';
$check_url .= isset($parsed['host']) ? $parsed['host'] : '';
$check_url .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
$check_url .= isset($parsed['path']) ? $parsed['path'] : '';
if ( !in_array($check_url, $valid_requests) ) {
exit;
}
}
// append query string for GET requests
if ( $request_method == 'GET' && count($request_params) > 0 && ( !array_key_exists('query', $p_request_url) || empty($p_request_url['query']) ) ) {
$request_url .= '?'. http_build_query($request_params);
}
// let the request begin
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); // (re-)send headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return response
curl_setopt($ch, CURLOPT_HEADER, true); // enabled response headers
// add post data for POST requests
if ( $request_method == 'POST' ) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_params));
}
// retrieve response (headers and content)
$response = curl_exec($ch);
curl_close($ch);
// split response to header and content
list($response_headers, $response_content) = preg_split('/(\r\n){2}/', $response, 2);
// (re-)send the headers
$response_headers = preg_split('/(\r\n){1}/', $response_headers);
foreach ( $response_headers as $key => $response_header )
if ( !preg_match('/^(Transfer-Encoding):/', $response_header) )
header($response_header);
// finally, output the content
print($response_content);
?>
再次,如果我在我的网站中输入http://mywebsite.com/ajax-proxy.php?csurl=www.google.com。它工作正常。甚至把它放在网址中。工作正常。但是如果您使用 ajax post 从外部服务器调用它。没用。