jQuery Ajax请求通过代理404'ing

时间:2015-04-21 14:16:36

标签: php jquery ajax proxy

我在通过代理运行jQuery Ajax请求时遇到了一些问题,但我不确定问题是什么。

我有以下index.php代码:

<div id="container">:(</div>

<form action="proxy.php" method="post">
    <input type="hidden" name="address"
        value="http://www.bungie.net/Platform/Destiny/2/Account/4611686018433330092/" />
    <input type="submit" name="submit" value="Submit" />
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/destiny.js"></script>

这会运行一小段jQuery(js / destiny.js):

$.ajax({
url: 'proxy.php',
type: 'POST',
dataType: "json",
data: {
    address: '/Platform/Destiny/2/Account/4611686018433330092/'
},
success: function(response) {
    console.log(response);
    // response now contains full HTML of google.com
    $("#container").html(':D');
},
error: function(req, status, errThrown){
    // error handling
    console.log("req: " + req);
    console.log("status: " + status);
    console.log("errThrown: " + errThrown);
}

});

我的proxy.php只是Google代码PHP代理,修改了打印行:

    <?php
/*
 * Author - Rob Thomson <rob@marotori.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

session_start();
ob_start();

ini_set('display_errors', 0);

/* config settings */
$base = "http://www.bungie.net";  //set this to the url you want to scrape
$ckfile = 'tmp/simpleproxy-cookie-'.session_id();  //this can be set to anywhere you fancy!  just make sure it is secure.

/* all system code happens below - you should not need to edit it! */

//work out cookie domain
$cookiedomain = str_replace("http://www.","",$base);
$cookiedomain = str_replace("https://www.","",$cookiedomain);
$cookiedomain = str_replace("www.","",$cookiedomain);

$url = $base . $_SERVER['REQUEST_URI'];

if($_SERVER['HTTPS'] == 'on'){
    $mydomain = 'https://'.$_SERVER['HTTP_HOST'];
} else {
    $mydomain = 'http://'.$_SERVER['HTTP_HOST'];
}

// Open the cURL session
$curlSession = curl_init();

curl_setopt ($curlSession, CURLOPT_URL, $url);
curl_setopt ($curlSession, CURLOPT_HEADER, 1);


if($_SERVER['REQUEST_METHOD'] == 'POST'){
    curl_setopt ($curlSession, CURLOPT_POST, 1);
    curl_setopt ($curlSession, CURLOPT_POSTFIELDS, $_POST);
}

curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curlSession, CURLOPT_TIMEOUT,30);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt ($curlSession, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($curlSession, CURLOPT_COOKIEFILE, $ckfile);

//handle other cookies cookies
foreach($_COOKIE as $k=>$v){
    if(is_array($v)){
        $v = serialize($v);
    }
    curl_setopt($curlSession,CURLOPT_COOKIE,"$k=$v; domain=.$cookiedomain ; path=/");
}

//Send the request and store the result in an array
$response = curl_exec ($curlSession);

// Check that a connection was made
if (curl_error($curlSession)){
    // If it wasn't...
    print curl_error($curlSession);
} else {

    //clean duplicate header that seems to appear on fastcgi with output buffer on some servers!!
    $response = str_replace("HTTP/1.1 100 Continue\r\n\r\n","",$response);

    $ar = explode("\r\n\r\n", $response, 2);

    $header = $ar[0];
    $body = $ar[1];

    //handle headers - simply re-outputing them
    $header_ar = split(chr(10),$header);
    foreach($header_ar as $k=>$v){
        if(!preg_match("/^Transfer-Encoding/",$v)){
            $v = str_replace($base,$mydomain,$v); //header rewrite if needed
            header(trim($v));
        }
    }

    //rewrite all hard coded urls to ensure the links still work!
    $body = str_replace($base,$mydomain,$body);

    #print $body;
    echo file_get_contents($_POST['address']);

}

curl_close ($curlSession);

?>

如果我在index.php文件中提交测试表单,我得到的数据没有问题,但是当通过Ajax运行相同的请求时,我得到了404.是否有人能够向我显示我的方式错误?

0 个答案:

没有答案