使用Pest PHP类的API调用返回错误的结果

时间:2013-06-02 19:16:25

标签: php api rest

我正在考虑使用http://rolz.org/api/?4d20Pest PHP REST client上对网络应用程序进行最基本的API调用。使用Chrome插件REST客户端,我得到了预期的结果而没有错误:

result=45
details= [ 16 +20 +3 +6  ] 
code=4d20
illustration=<span class="dc_dice_a">4</span><span class="dc_dice_d">D20</span>
timestamp=1370200094

但是,使用Pest PHP REST客户端,我的结果前面会显示一条错误消息:

string $rolldice = result=Error: please check your formula (/52)

details=/ [ 9 +16 +20 +7  ] 
code=/4d20
illustration=<span class="dc_operator">/</span><span class="dc_dice_a">4</span><span class="dc_dice_d">D20</span>
timestamp=1370200381

使用此代码:

include '../Pest.php';

function callDieRoller($num, $faces){
    $result = array();
    $curl = curl_init();

    $url = 'http://rolz.org/api/?';
    $pest = new Pest($url);

    $rolldice = $pest->get($num.'d'.$faces);
    $results = $rolldice;

    return $results;

}

为什么在与Pest进行API调用时会出现错误?

1 个答案:

答案 0 :(得分:1)

这是因为Pest确保了基本api网址和被叫网址之间的/,因此您调用http://rolz.org/api/?/4d20之类的内容。要使其正常工作,您必须定义没有问号的基本网址,并在每次通话前添加它:

$url = 'http://rolz.org/api/';
$pest = new Pest($url);

$rolldice = $pest->get('?'.$num.'d'.$faces);
$results = $rolldice;