我正在尝试执行ajax请求以从服务器读取xml。我正在使用代理来执行此操作。如果我直接在浏览器中输入请求,它将返回正确的XML。当我使用我的代理时,它返回“无效的参数”。有什么想法吗?
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 :(得分:0)
根据您提供的信息,我猜测服务器期望您的标头或Cookie数据中有更多数据。在Ajax页面上抓取数据时,最好使用Curl Library而不是file_get_contents()。 当您直接在浏览器中输入请求时,请使用firefox firebug扩展中的Net选项来确切了解标题中传递的内容。复制这些标头并在CURL中设置它们。如果这不起作用,则可能是cookie问题。让CURL访问原始页面,存储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);
?>
我忘了:您可以使用CURL中的CURLOPT_PROXY选项来设置代理