【发布时间】:2013-10-10 18:26:09
【问题描述】:
谁能告诉我如何从 REST webservice 获取 json。
我的代码如下
在这里当我们运行应用程序时,如果给出例子
localhost/examples/Example.php?method=addInts&n1=3&n2=5
会给我们输出
{"Result":"3 + 5 = 8"}
我们怎么能期望像json一样返回输出
[{"Result":8}]
我的php代码
<?php
require_once "../Rest.php";
class Hello
{
// example: http://path/to/examples/Example.php?method=sayHello&name=World
public static function sayHello($name)
{
return array("Response" => "Hello, " . $name);
}
// example: http://path/to/examples/Example.php?method=addInts&n1=3&n2=5
public static function addInts($n1, $n2)
{
if (is_numeric($n1) && is_numeric($n2))
{
return array("Result" => "$n1 + $n2 = " . (string)($n1 + $n2));
}
else
{
return array("Error" => "Parameters must be numeric.");
}
}
}
$rest = new Server(Hello);
$rest->handle();
?>
【问题讨论】:
标签: php json web-services rest