[N long ago published article] php get yahoo stock data and csv json data interface output to be closed

Thinking

Yahoo start acquiring data interfaces, then the interface is defined, converted into json output. Only for Cartoon Network (kt5.cn) use

Interface processing code stock.php






= $ StartDateArr [1] - 1 ;
$ StartDay = $ StartDateArr [2 ];
$ Start Year = $ StartDateArr [0 ];
$ EndMonth = $ EndDateArr [1] - 1 ;
$ EndDay = $ EndDateArr [2 ];
$ Endyear = $ EndDateArr [0 ];
$ StockAPI = 'http://table.finance.yahoo.com/table.csv?s='. $ Stock Name . '& F ='. $ Endyear '= d'.. $ EndMonth . '& E ='. $ EndDay . '& C ='. $ Start Year . '& A ='... '& B =' $ StartDay . '& G = D & the ignore = .csv'; // definition of stock api the URL 
$ StockData = json_encode (csv_in_array ( $ StockAPI , ",", "", to true )); // a defined value Stock of the read information 
// echo '<pre>'; // output format of the present description 
echo  $ StockData ; // output text information acquired stock 
// echo "</ pre>" ; // output the original text format 

function csv_in_array ( $ URL , $ DELM = ",", $ Encl = "", $ head = to true ) { 
    
    $ csvxrow = File ( $ URL );   // ---- csv rows to array ----
    
    $csvxrow[0] = chop($csvxrow[0]); 
    $csvxrow[0] = str_replace($encl,'',$csvxrow[0]); 
    $keydata = explode($delm,$csvxrow[0]); 
    $keynumb = count($keydata); 
    
    if ($head === true) { 
    $anzdata = count($csvxrow); 
    $z=0; 
    for($x=1; $x<$anzdata; $x++) { 
        $csvxrow[$x] = chop($csvxrow[$x]); 
        $csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]); 
        $csv_data[$x] = explode($delm,$csvxrow[$x]); 
        $i=0; 
        foreach($keydata as $key) { 
            $out[$z][$key] = $csv_data[$x][$i]; 
            $i++;
            }    
        $z++;
        }
    }
    else { 
        $i=0;
        foreach($csvxrow as $item) { 
            $item = chop($item); 
            $item = str_replace($encl,'',$item); 
            $csv_data = explode($delm,$item); 
            for ($y=0; $y<$keynumb; $y++) { 
               $out[$i][$y] = $csv_data[$y]; 
            }
        $i++;
        }
    }

return $out; 
}


?>

 

Guess you like

Origin www.cnblogs.com/fastmover/p/11422730.html