2017-01-06 23 views
1

在现有的非OOP网站上实现php-ews时未找到类“SoapClient”。php-ews:在使用完整路径到类

在shell中运行独立PHP脚本正常工作。

独立的PHP脚本使用use

use \jamesiarmes\PhpEws\Client; 
use \jamesiarmes\PhpEws\Request\CreateItemType; 

但我不能在网站上使用use内if语句,所以我使用前置完整路径的所有类(即new \jamesiarmes\PhpEws\Client)。

现在我收到此错误。

Fatal error: Class 'SoapClient' not found in /home/project/master/vendor/jamesiarmes/php-ntlm/src/SoapClient.php on line 13

我验证安装的SOAP扩展。请帮忙。谢谢。在外壳

if ($_REQUEST['action'] == 'export-form-test') { 

    require_once 'vendor/autoload.php'; 

/* 
use \jamesiarmes\PhpEws\Client; 
use \jamesiarmes\PhpEws\Request\CreateItemType; 

use \jamesiarmes\PhpEws\ArrayType\NonEmptyArrayOfAllItemsType; 
use \jamesiarmes\PhpEws\ArrayType\NonEmptyArrayOfAttendeesType; 

use \jamesiarmes\PhpEws\Enumeration\BodyTypeType; 
use \jamesiarmes\PhpEws\Enumeration\CalendarItemCreateOrDeleteOperationType; 
use \jamesiarmes\PhpEws\Enumeration\ResponseClassType; 
use \jamesiarmes\PhpEws\Enumeration\RoutingType; 

use \jamesiarmes\PhpEws\Type\AttendeeType; 
use \jamesiarmes\PhpEws\Type\BodyType; 
use \jamesiarmes\PhpEws\Type\CalendarItemType; 
use \jamesiarmes\PhpEws\Type\EmailAddressType; 
*/ 

    // Replace this with your desired start/end times and guests. 
    $start = new DateTime('tomorrow 4:00pm'); 
    $end = new DateTime('tomorrow 5:00pm'); 

    // Set connection information. 
    $host = '*********'; 
    $username = '*******'; 
    $password = '********'; 
    //$version = Client::VERSION_2016; 

    $client = new \jamesiarmes\PhpEws\Client($host, $username, $password); 

    // Build the request, 
    $request = new \jamesiarmes\PhpEws\Request\CreateItemType(); 
    $request->SendMeetingInvitations = \jamesiarmes\PhpEws\Enumeration\CalendarItemCreateOrDeleteOperationType::SEND_ONLY_TO_ALL; 
    $request->Items = new \jamesiarmes\PhpEws\ArrayType\NonEmptyArrayOfAllItemsType(); 

    // Build the event to be added. 
    $event = new \jamesiarmes\PhpEws\Type\CalendarItemType(); 
    $event->Start = $start->format('c'); 
    $event->End = $end->format('c'); 
    $event->title = 'EWS Test Event'; 

    // Set the event body. 
    $event->Body = new \jamesiarmes\PhpEws\Type\BodyType(); 
    $event->Body->_ = 'This is the event body'; 
    $event->Body->BodyType = \jamesiarmes\PhpEws\Enumeration\BodyTypeType::TEXT; 

    // Add the event to the request. You could add multiple events to create more 
    // than one in a single request. 
    $request->Items->CalendarItem[] = $event; 

    $response = $client->CreateItem($request); 

    // Iterate over the results, printing any error messages or event ids. 
    $response_messages = $response->ResponseMessages->CreateItemResponseMessage; 
    foreach($response_messages as $response_message) { 
     // Make sure the request succeeded. 
     if ($response_message->ResponseClass != ResponseClassType::SUCCESS) { 
      $return_arr['status'] = 'error'; 
      continue; 
     } 

     // Iterate over the created events, printing the id for each. 
     foreach($response_message->Items->CalendarItem as $item) { 
      $id = $item->ItemId->Id; 
      $return_arr['status'] = 'ok'; 
     } 
    } 

} 
+0

服务器和机器上的PHP版本是什么? 'vendor/autoload.php'文件来自哪里?你能否包含或链接其源代码? –

回答

1

独立运行PHP脚本正常工作。

我验证安装的SOAP扩展。

您是如何验证的?您知道您可能正在为CLI和Web服务器环境加载不同的php.ini文件。

(例如,在我的Ubuntu的服务器,使用Apache Web服务器,我都

/etc/php5/cli/php.ini

/etc/php5/apache2/php.ini

+1

php-fpm没有正确重启,而CLI没问题。愚蠢的...... =) –

1

不回答你原来的问题,但是这一个:

但是我不能使用use ide if语句在网站上,所以我预先使用完整路径的所有类(即new \ jamesiarmes \ PhpEws \ Client)。

use要导入命名空间类的语句属于文件的顶部,在任何括号之外。它们总是被“执行”,即在外部类名称的当前名称空间内创建别名链接(如果没有使用名称空间,则使用根名称空间) - 仅用于它们所在的当前文件。什么都不是永久的或者不在当前文件之外 - 新文件可能会有不同的导入,但如果两个文件都使用了类,它们也必须重复任何导入。

请参阅http://php.net/manual/en/language.namespaces.php了解有关名称空间的章节,http://php.net/manual/en/language.namespaces.importing.php特别是使用use。注意实施例5用非法的代码:

<?php 
namespace Languages; 

function toGreenlandic() 
{ 
    use Languages\Danish; 

    // ... 
} 
?> 

导入类与use独立于的代码的执行。它已经在分析文件时发生,以允许PHP知道代码中使用的所有类的完全限定名。它们只会在代码执行时自动加载,但use应保留在文件的顶部。

+0

如果我不使用'function(){}'会怎么样? '语言'命名空间必须与'vendor/autoload'预先加载的内容匹配吗? –

+0

'use'不能在函数,类或块中使用。而且你总是必须提供完整的命名空间,没有捷径可能。但是,在使用'use'后,你可以通过它的名字引用导入的类,而不用引导反斜杠,或者如果你输入了别名,就可以引用它。 – Sven