0
votes

I'm trying to convert Csv to Json with Php.

This is the Csv source: finance.sina.com. hk/cgi-bin/api/stock.cgi?action=stock&s=1
The value of 's', like example ablove: s=1, can be substituted with any number in order to recieve different stock's data.

And I've done this part (truboy.comeze. com/csv2json.php) :

$set = http_build_query( array( 'action' => 'stock', 's' => '1' ) );

$opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $set ) );

$url = "http://finance.sina.com.hk/cgi-bin/api/stock.cgi";

$context = stream_context_create($opts);

$result = file_get_contents($url, false, $context);

echo $result;

This code actually works. However, the value 's' is fixed with '1' and I want 's' to be changeable

Can I do sth. like 'posting data' e.g.csv2json.php?s=123 ?

Thanks guys!

2

2 Answers

0
votes

How do you want it to be changed?

$feed = 123;
$set = http_build_query( array( 'action' => 'stock', 's' => $feed ) );

or something like

$set = http_build_query( array( 'action' => 'stock', 's' => (int)$_POST ) );
0
votes

Oops, I asked a wrong question, it should be 'get' not 'post' Anyway, i find my solution

   's' => (int)$_GET['s']