-2

我有 1 个具有 1 个参数的函数。我想从浏览器调用这个函数。我怎么打电话?它给了我空白。当我在 function 之外编写相同的代码时,它正在工作。请帮我。我将此链接传递给浏览器“域名://ipaddress/test.php/mywebservice”

我正在使用此代码:

<?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;
}
?>

结果 :

{'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; } ?>

如果我写:

<?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":""},]

哪个是对的。

4

1 回答 1

1

从......(让我引用你的话)“外部函数”调用它。

例如:page.php:

include "file-containing-my-function.php";
$param = $_GET['param'];
myFunction($param);

您可以使用更通用且安全性更低的方法来传递函数名称:

请求看起来像这样:(或使用重写引擎,您可以从请求page.php?action=myFunction&param=value 中获得相同的 URL )://domain/page/myFunction/value

页面.php:

$callback = $_GET['action'];
$param = $_GET['value'];
echo $callback($param);

使用此方法,您可以使用任何第一个或单个参数从您的服务中调用任何函数,而只需更改 URL。但是您也暴露了所有内容,因此您应该通过根据用户权限检查函数名称或检查您拥有的每个函数的权限来保护此请求。函数的参数也是如此。

没有直接的方法来指定要从 URL 调用的函数。如果您需要这样的功能,您需要自己实现它,就像我在上面所做的那样。

编辑:

我想我明白你的问题是什么了。使用重写引擎。添加到您的 .htaccess

RewriteEngine on
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^test.php/(.*)?$ test.php?function=$1&%1 [L]

这将重写一个查询,如:

http://domain/page.php/myFunction?param=value

http://domain/page.php?function=myFunction¶m=value

请注意,myFunction 也可以变为不可调用的“asd/asd/asd”(对于像“page.php/asd/asd/asd?asd”这样的请求)。

于 2012-09-28T06:24:58.757 回答