I have 1 function which is having 1 parameter.
I want to call this function from browser.
How can I call?
It is giving me blank.
When I write same code outside function , it is working.
Please help me.
I am passing this link to browser “domainname ://ipaddress/test.php/mywebservice”
i am using this code :
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
//print $obj->{'foo-bar'}; // 12345
switch($_GET['function']) {
case 'specificFunction':
abc();
}
function abc
{
$con = mysql_connect("localhost","uname","Password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//print_r($con);
mysql_select_db("roster", $con);
$query = "select * from rates";
$rs = mysql_query($query) or die($query);
//print_r($rs);
while($row=mysql_fetch_assoc($rs)){
$record[] = $row;
}
$data = json_encode($record);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo $data;
}
?>
Result :
{'foo-bar'}; // 12345 switch($_GET['function']) { case 'specificFunction': abc(); } function abc { $con = mysql_connect("localhost","uname","Password"); if (!$con) { die('Could not connect: ' . mysql_error()); } //print_r($con); mysql_select_db("roster", $con); $query = "select * from rates"; $rs = mysql_query($query) or die($query); //print_r($rs); while($row=mysql_fetch_assoc($rs)){ $record[] = $row; } $data = json_encode($record); header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); echo $data; } ?>
If I wirte:
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
//print $obj->{'foo-bar'}; // 12345
$con = mysql_connect("localhost","uname","Password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//print_r($con);
mysql_select_db("roster", $con);
$query = "select * from rates";
$rs = mysql_query($query) or die($query);
//print_r($rs);
while($row=mysql_fetch_assoc($rs)){
$record[] = $row;
}
$data = json_encode($record);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo $data;
?>
Result :
[{"Month":"July","Year":"2012","Rate":"1.20%","Short":"0.24%","Mid":"0.92%","Long":"2.30%","RateUnFormatted":"0.012","LastUpdated":"2012-09-01 01:00:00","PublishFlag":"M"},{"Month":"June","Year":"2012","Rate":"1.20%","Short":"0.23%","Mid":"1.07%","Long":"2.64%","RateUnFormatted":"0.012","LastUpdated":"0000-00-00 00:00:00","PublishFlag":""},]
Which is correct.
Call it from… (let me quote you) “outside function”.
e.g.:
page.php:
You can use a more general and much less secure method with passing the function name:
With a request looking something like this:
page.php?action=myFunction¶m=value(or with rewrite engine you can achieve this same URL from a
://domain/page/myFunction/valuerequest)page.php:
With this method you can call any function from your service with any first or single parameter with just changing the URL. But you also expose everything, so you should secure this request, by either checking the function’s name against user privileges, or check privileges in every single function you have. Same goes for the parameter of the function.
There’s no direct way to specify a function to be called from a URL. You need to implement it for yourself, if you need a functionality like this, just as I did above.
EDIT:
I think I see what’s your question. Use rewrite engine. Add to your .htaccess
This will rewrite a query like:
http://domain/page.php/myFunction?param=value
to
http://domain/page.php?function=myFunction¶m=value
Note that myFunction can also become “asd/asd/asd” (for a request like “page.php/asd/asd/asd?asd”) which is not a callable.