Simple Web Service Using PHP + MYQSQL + JSON + CURL. Part 2 - Server
1. Create a function.
2. Pay attention to this line -> echo json_encode($post). This will convert array into json format.
3. Get request parameter from client.
4. Final code.
Next - Simple Web Service Using PHP + MYQSQL + JSON + CURL. Part 3 - Client
function returnAllPrices()
{
$link = mysql_connect("localhost","root", "") or die('Could not connect: ' . mysql_error());
mysql_select_db("soap", $link);
$query = "select * from stockprices";
$result = mysql_query($query, $link);
$post = array();
$counter = 0;
while($row = mysql_fetch_array($result))
{
$post[$counter][0] = $row["stock_id"];
$post[$counter][1] = $row["stock_symbol"];
$post[$counter][2] = $row["stock_price"];
$counter++;
}
echo json_encode($post);
}
2. Pay attention to this line -> echo json_encode($post). This will convert array into json format.
3. Get request parameter from client.
$request = $_GET["request"];
4. Final code.
<?php5. Save this file as server.php
function returnAllPrices()
{
$link = mysql_connect("localhost","root", "") or die('Could not connect: ' . mysql_error());
mysql_select_db("soap", $link);
$query = "select * from stockprices";
$result = mysql_query($query, $link);
$post = array();
$counter = 0;
while($row = mysql_fetch_array($result))
{
$post[$counter][0] = $row["stock_id"];
$post[$counter][1] = $row["stock_symbol"];
$post[$counter][2] = $row["stock_price"];
$counter++;
}
echo json_encode($post);
}
$request = $_GET["request"];
if($request == "allprices")
{
returnAllPrices();
}
?>
Next - Simple Web Service Using PHP + MYQSQL + JSON + CURL. Part 3 - Client
Post a Comment