0

wordnik 的 php api 致命错误出现以下错误:在第 212 行的 Swagger.php 中找不到类“示例”

我编辑了 Swagger.php 让它工作,下面是原始函数

function swagger_autoloader($className) {
    $currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
    if (file_exists($currentDir . '/' . $className . '.php')) {
        include $currentDir . '/' . $className . '.php';
    } elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
        include $currentDir . '/models/' . $className . '.php';
    }
}

我改成的工作代码是

function swagger_autoloader($className) 
{
        include $currentDir . '/models/' . $className . '.php'; 
}

更多信息

我的文件结构如下。WORDNIK(包含 start.php)>>>WORDNIK(包含 Swagger.php)>>MODELS(包含 Example.php)

  • 我正在使用 wampserver 2.2 和 php 5.4.3

更新/编辑

使用以下代码

function swagger_autoloader($className) {
    echo "dirname(__FILE__)= ",dirname(__FILE__);
    $currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
    echo "currentDir=".$currentDir."</br>";
    echo "className=".$className."</br>";
    echo "__FILE__=",__FILE__."<br/>";
    die();

我得到结果

  • dirname(__FILE__)=D:\wamp\www\wordnik\wordnik
  • 当前目录=
  • 类名=示例
  • __FILE__=D:\wamp\www\wordnik\wordnik\Swagger.php

正如 vcampitelli 所建议的,使用其中一个__DIR__dirname(__FILE__)工作。

我的问题

为什么原来的功能不起作用?

不重要的信息

对于那些在示例中苦苦挣扎的人,这是我的 start.php 文件

<?php
require('./wordnik/Swagger.php');
require('./wordnik/WordApi.php');
require('./wordnik/AccountApi.php');
require('./wordnik/WordsApi.php');
require('./wordnik/WordListApi.php');
require('./wordnik/WordListsApi.php');

$myAPIKey = 'replace_this_with_your_real_api';
$client = new APIClient($myAPIKey, 'http://api.wordnik.com/v4');

$wordApi = new WordApi($client);
$example = $wordApi->getTopExample('irony');
print $example->text;
?>
4

2 回答 2

2

返回什么$currentDir?您是否尝试过使用__DIR__or dirname(__FILE__)(它们是相同的)而不是 that substr

如果您正在使用第二个示例,就像您发布的那样(没有声明$currentDir),那么这就是您原始代码的问题:$currentDir没有返回正确的文件夹!

使用原始代码,包含哪个文件?因为,实际上,我认为没有人是!echo在这些语句中使用if来检查!

于 2013-03-03T14:54:02.337 回答
1

原始函数不起作用,因为它正在寻找 UNIX 样式的正斜杠“/”的位置,而您在 Windows 上并且有反斜杠“\”。那是一个错误!我将修复 lib 以像您一样使用 dirname( FILE )。感谢您指出错误。

于 2013-03-06T08:41:39.337 回答