2012-01-27 69 views
0

我正在尝试执行ajax请求以从服务器读取xml。我正在使用代理来执行此操作。如果我直接在浏览器中输入请求,它将返回正确的XML。当我使用我的代理时,它返回“无效参数”。任何想法?AJAX返回“无效参数”的请求,请求在没有代理的情况下工作

Proxy.PHP

<?php 
$c = file_get_contents((urldecode($_REQUEST['u']))); 
$content_type = 'Content-Type: text/plain'; 
for ($i = 0; $i < count($http_response_header); $i++) { 
    if (preg_match('/content-type/i',$http_response_header[$i])) { 
     $content_type = $http_response_header[$i]; 
    } 
} 
if ($c) { 
    header($content_type); 
    echo $c; 
} 
else { 
    header("content-type: text/plain"); 
    echo 'There was an error satisfying this request.'; 
} 
?> 

要求:

$.ajax({ 
     type: "GET", 
     url: 'proxy.php?u=' + 'http://192.168.100.147:8080/thredds/sos/cfpoint/timeSeriesProfile-Ragged-MultipeStations-H.5.3/timeSeriesProfile-Ragged-MultipeStations-H.5.3.nc?request=GetObservation&service=SOS&version=1.0.0&responseFormat=text%2Fxml%3B%20subtype%3D%22om%2F1.0.0%22&offering=urn:tds:station.sos:Station1&procedure=urn:tds:station.sos:Station1&observedproperty=temperature&eventTime=1990-01-01T00:00:00Z/1990-01-01T00:00:00Z', 
     dataType: "xml", 
     success: parseSOSGetObs, 
     error: function() {alert("AJAX ERROR for " + capRequest);} 
}); 

谢谢!

回答

0

根据您提供的信息,我猜想服务器期望您的标头或cookie数据中有更多数据。在Ajax页面上抓取数据时,最好使用Curl Library而不是file_get_contents()。 当您直接将请求输入到浏览器中时,请使用firefox firebug扩展中的Net选项准确查看标题中传递的内容。复制这些标题并将它们设置为CURL。如果这不起作用,这可能是一个cookie问题。带有卷曲访问原来的页面,存储的cookie,并利用它们的第二个请求..

例:

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/COOKIE"); 
curl_setopt($ch, CURLOPT_URL, "http://originalsite.com/"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0a2) Gecko/20111101 Firefox/9.0a2"); 
$junk = curl_exec($ch); 
curl_close($ch); 


$headers = array("X-Prototype-Version: 1.6.0", "X-Requested-With: XMLHttpRequest"); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/COOKIE"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/COOKIE"); 
curl_setopt($ch, CURLOPT_URL, "http://originalsite.com/setsomething.sync"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_REFERER, "http://originalsite.com/"); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0a2) Gecko/20111101 Firefox/9.0a2"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$page = curl_exec($ch); 
curl_close($ch); 
?> 

我忘了:你可以使用CURLOPT_PROXY选项卷曲设置代理