保存腳本服務器上
parse.php
<?php
$response =array('result'=>'failed','message'=>'missing params');
if(isset($_GET['from']) && isset($_GET['to'])){
$from = $_GET['from'];
$to = $_GET['to'];
$url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from .'&X='. $to;
$handle = @fopen($url, 'r');
if ($handle) {
$result = fgets($handle, 4096);
fclose($handle);
}
$allData = explode(',',$result);
$dollarValue = $allData[1];
$response['result']=$dollarValue;
$response['message']="value sent";
}
echo json_encode($response);
?>
JavaScript方法
function getData(from,to){
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
xhr1 = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
xhr1 = new ActiveXObject("Microsoft.XMLHTTP");
}
//path to your script
xhr1.open("GET", "http://localhost/practice/parse.php?from="+from+"&to="+to, true);
xhr1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr1.send();
xhr1.onreadystatechange = display_data;
function display_data() {
if (xhr1.readyState == 4) {
console.log(xhr1.responseText);
//do what you want to do here
}
}
}
這是正確的? –
$ url ='http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='。 $從。 $ to。'= X'; –
@Azmatkarim我已經手動輸入了包含在瀏覽器中的變量的URL,並且它工作正常 – user3476732