2016-09-29 46 views
1

我将快速书本与我的laravel应用程序集成在一起。整合后我得到这个错误,在Laravel 5.3中导入Quickbook库时出错

PHP Warning: require_once(../QuickBooks.php): failed to open stream: No such file or directory in /home/vipin/projects/development/Quickbook/config/app.php on line 2 PHP Fatal error: require_once(): Failed opening required '../QuickBooks.php' (include_path='.:/usr/share/php:/home/ubuntu/projects/development/Quickbook/vendor/consolibyte/quickbooks') in /home/ubuntu/projects/development/Quickbook/config/app.php on line 2

这里是我的控制器Quickbook.php

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
// require_once '../QuickBooks.php'; 

use App\Http\Requests; 

class QuickBooksController extends Controller 
{ 
private $IntuitAnywhere; 
private $context; 
private $realm; 

public function __construct(){ 
    if (!\QuickBooks_Utilities::initialized(env('QBO_DSN'))) { 
     // Initialize creates the neccessary database schema for queueing up requests and logging 
     \QuickBooks_Utilities::initialize(env('QBO_DSN')); 
    } 
    $this->IntuitAnywhere = new \QuickBooks_IPP_IntuitAnywhere(env('QBO_DSN'), env('QBO_ENCRYPTION_KEY'), env('QBO_OAUTH_CONSUMER_KEY'), env('QBO_CONSUMER_SECRET'), env('QBO_OAUTH_URL'), env('QBO_SUCCESS_URL')); 
} 

public function qboConnect(){ 
    if ($this->IntuitAnywhere->check(env('QBO_USERNAME'), env('QBO_TENANT')) && $this->IntuitAnywhere->test(env('QBO_USERNAME'), env('QBO_TENANT'))) { 
     // Set up the IPP instance 
     $IPP = new \QuickBooks_IPP(env('QBO_DSN')); 
     // Get our OAuth credentials from the database 
     $creds = $this->IntuitAnywhere->load(env('QBO_USERNAME'), env('QBO_TENANT')); 
     // Tell the framework to load some data from the OAuth store 
     $IPP->authMode(
      \QuickBooks_IPP::AUTHMODE_OAUTH, 
      env('QBO_USERNAME'), 
      $creds); 

     if (env('QBO_SANDBOX')) { 
      // Turn on sandbox mode/URLs 
      $IPP->sandbox(true); 
     } 
     // This is our current realm 
     $this->realm = $creds['qb_realm']; 
     // Load the OAuth information from the database 
     $this->context = $IPP->context(); 

     return true; 
    } else { 
     return false; 
    } 
} 

public function qboOauth(){ 
    if ($this->IntuitAnywhere->handle(env('QBO_USERNAME'), env('QBO_TENANT'))) 
    { 
     ; // The user has been connected, and will be redirected to QBO_SUCCESS_URL automatically. 
    } 
    else 
    { 
     // If this happens, something went wrong with the OAuth handshake 
     die('Oh no, something bad happened: ' . $this->IntuitAnywhere->errorNumber() . ': ' . $this->IntuitAnywhere->errorMessage()); 
    } 
} 

public function qboSuccess(){ 
    return view('qbo_success'); 
} 

public function qboDisconnect(){ 
    $this->IntuitAnywhere->disconnect(env('QBO_USERNAME'), env('QBO_TENANT'),true); 
    return redirect()->intended("/yourpath");// afer disconnect redirect where you want 

} 

public function createCustomer(){ 

    $CustomerService = new \QuickBooks_IPP_Service_Customer(); 

    $Customer = new \QuickBooks_IPP_Object_Customer(); 
    $Customer->setTitle('Ms'); 
$Customer->setGivenName('Shannon'); 
$Customer->setMiddleName('B'); 
$Customer->setFamilyName('Palmer'); 
$Customer->setDisplayName('Shannon B Palmer ' . mt_rand(0, 1000)); 
    // Terms (e.g. Net 30, etc.) 
    $Customer->setSalesTermRef(4); 

    // Phone # 
    $PrimaryPhone = new \QuickBooks_IPP_Object_PrimaryPhone(); 
    $PrimaryPhone->setFreeFormNumber('860-532-0089'); 
$Customer->setPrimaryPhone($PrimaryPhone); 

    // Mobile # 
    $Mobile = new \QuickBooks_IPP_Object_Mobile(); 
    $Mobile->setFreeFormNumber('860-532-0089'); 
$Customer->setMobile($Mobile); 

    // Fax # 
    $Fax = new \QuickBooks_IPP_Object_Fax(); 
    $Fax->setFreeFormNumber('860-532-0089'); 
$Customer->setFax($Fax); 

    // Bill address 
    $BillAddr = new \QuickBooks_IPP_Object_BillAddr(); 
    $BillAddr->setLine1('72 E Blue Grass Road'); 
$BillAddr->setLine2('Suite D'); 
$BillAddr->setCity('Mt Pleasant'); 
$BillAddr->setCountrySubDivisionCode('MI'); 
$BillAddr->setPostalCode('48858'); 
$Customer->setBillAddr($BillAddr); 

    // Email 
    $PrimaryEmailAddr = new \QuickBooks_IPP_Object_PrimaryEmailAddr(); 
    $PrimaryEmailAddr->setAddress('[email protected]'); 
    $Customer->setPrimaryEmailAddr($PrimaryEmailAddr); 

    if ($resp = $CustomerService->add($this->context, $this->realm, $Customer)) 
    { 
     //print('Our new customer ID is: [' . $resp . '] (name "' . $Customer->getDisplayName() . '")'); 
     //return $resp; 
     //echo $resp;exit; 
     //$resp = str_replace('{','',$resp); 
     //$resp = str_replace('}','',$resp); 
     //$resp = abs($resp); 
     return $this->getId($resp); 
    } 
    else 
    { 
     //echo 'Not Added qbo'; 
     print($CustomerService->lastError($this->context)); 
    } 
} 

public function addItem(){ 
    $ItemService = new \QuickBooks_IPP_Service_Item(); 

    $Item = new \QuickBooks_IPP_Object_Item(); 

    $Item->setName('My Item'); 
$Item->setType('Inventory'); 
$Item->setIncomeAccountRef('53'); 

    if ($resp = $ItemService->add($this->context, $this->realm, $Item)) 
    { 
     return $this->getId($resp); 
    } 
    else 
    { 
     print($ItemService->lastError($this->context)); 
    } 
} 

public function addInvoice($invoiceArray,$itemArray,$customerRef){ 

    $InvoiceService = new \QuickBooks_IPP_Service_Invoice(); 

    $Invoice = new \QuickBooks_IPP_Object_Invoice(); 

    $Invoice = new QuickBooks_IPP_Object_Invoice(); 

$Invoice->setDocNumber('WEB' . mt_rand(0, 10000)); 
$Invoice->setTxnDate('2013-10-11'); 

$Line = new QuickBooks_IPP_Object_Line(); 
$Line->setDetailType('SalesItemLineDetail'); 
$Line->setAmount(12.95 * 2); 
$Line->setDescription('Test description goes here.'); 

$SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail(); 
$SalesItemLineDetail->setItemRef('8'); 
$SalesItemLineDetail->setUnitPrice(12.95); 
$SalesItemLineDetail->setQty(2); 

$Line->addSalesItemLineDetail($SalesItemLineDetail); 

$Invoice->addLine($Line); 

$Invoice->setCustomerRef('67'); 


    if ($resp = $InvoiceService->add($this->context, $this->realm, $Invoice)) 
    { 
     return $this->getId($resp); 
    } 
    else 
    { 
     print($InvoiceService->lastError()); 
    } 
} 

public function getId($resp){ 
    $resp = str_replace('{','',$resp); 
    $resp = str_replace('}','',$resp); 
    $resp = abs($resp); 
    return $resp; 
} 

} 

配置/ app.php

<?php 
require_once '../QuickBooks.php'; 

return [ 


    'qbo_token' => env('QUICKBOOK_TOKEN'), 
    'qbo_consumer_key' => env('QBO_OAUTH_CONSUMER_KEY'), 
    'qbo_consumer_secret' => env('QBO_CONSUMER_SECRET'), 
    'qbo_sandbox' => env('QBO_SANDBOX'), 
    'qbo_encryption_key' => env('QBO_ENCRYPTION_KEY'), 
    'qbo_username' => env('QBO_USERNAME'), 
    'qbo_tenant' => env('QBO_TENANT'), 
    'qbo_auth_url' => 'http://app.localhost:8000/qbo/oauth', 
    'qbo_success_url' => 'http://app.localhost:8000/qbo/success', 
    'qbo_mysql_connection' => 'mysqli://'. env('DB_USERNAME') .':'. env('DB_PASSWORD') .'@'. env('DB_HOST') .'/'. env('DB_DATABASE'), 
+1

“如果在include_path中找不到该文件,include将最终在调用脚本自己的目录和当前工作目录中检查失败前检查。”有没有什么回答 – cske

+0

我已经检查QuickBooks.php是存在于我的位置QuickBooks /供应商/ consolibyte/quickbooks/QuickBooks.php – vipin

+0

该路径看起来甚至远远不像你想要* 。什么**完全**是**完整路径**到'Config.app.php'和您试图包含的'QuickBooks.php'文件? – Phil

回答

0

有几个方面来这里与给定的代码&方法改进。

  1. 由于Anton correctly points out,你不应该直接要求任何quickbooks库文件。如果你已经通过Composer加载它,那么它们将被自动加载,因为Composer自动加载器将从供应商加载QuickBooks文件。这对于Laravel以及一般的基于Composer的应用程序是正确的 - 与Laravel唯一的区别在于没有为此SDK编写的特定Laravel Package ServiceProvider,但这并不重要。
  2. QuickBooks库试图自动加载任何以“QuickBooks”开头的类,所以最好为您的控制器类创建一个QuickBooks文件夹。这更像是一个“陷阱”,一直是pointed out in the repo issues
  3. 你得到Driver/.php错误的原因是因为你没有指定你的QBO_DSN,或者做得不正确 - 你传递给初始化的这个DSN环境变量在SDK代码中通过parse_url()运行,打开falsenull并打破自动装载机的初始化。如果将其设置为正确的连接字符串(例如mysqli://username:[email protected]:port/database并注意port必须是一个数字或它被认为是格式错误),它会正确处理DSN并继续加载该页面。请注意,初始化将尝试解析并获取主机的网络地址,因此您不能只在其中放入一个虚拟值,并期望它能够正常工作 - 这需要先存在。
  4. 您正在混合您的环境变量和应用程序配置,而没有正确使用它们中的任何一个。如果您希望将数据库连接字符串(又名QBO_DSN)构造成应用程序配置设置qbo_mysql_connection的特定方式,那么在尝试初始化/加载/ etc时应该是using the configuration setting。而不是使用env('QBO_DSN'),您应该使用config('app.qbo_mysql_connection')从您的应用设置加载构建的版本。通常情况下,您不会将太多环境变量加载到控制器中 - 应该由应用程序处理,然后控制器调用应用程序配置,以便它不知道如何定义它们。
  5. 您应该不需要require任何来自应用程序配置文件的内容 - 该文件仅用于设置配置变量。
  6. 由于QuickBooks SDK没有正确的名称空间(yet),因此没有很好的PSR-4加载方式(和use -ing),但在上面使用use子句仍是很好的做法该文件(例如use QuickBooks_Utilities;),以便您可以使用这些类,而不用担心忘记前一个反斜杠(即不再使用\QuickBooks_Utilities,仅使用QuickBooks_Utilities) - 给定代码中有几个实例已被遗忘,并且不会运行因为Laravel应用程序是命名空间的,并且将在App\Http\Controllers名称空间中查找这些类(例如,“找不到类App\Http\Controllers\QuickBooks_Utilities”等错误)。
  7. 缩进 - 选择一个样式(例如制表符,2-空间,PSR-2等),然后坚持。运行phpcs或其他清理工具,然后将其提交到您的存储库或在SO上发布 - 可读性非常重要!
+0

对于任何人低估了这一点,我将不胜感激,如果你可以留下评论与_why_你downvoted这个回应,所以我可以解决它,并尽可能更新答案。 – Leith

0

使用require而不是磁带自动加载机在现代框架(通常在现代PHP)中是一个糟糕的做法。我强烈建议使用软件包管理器(例如composer)将模块正确添加到项目中。

例如,使用作曲家添加QuickBooks的库到项目中,你需要运行只需要一个命令:

composer require consolibyte/quickbooks 
+0

我已经这样做了,但是没有与laravel(发布者)的适当集成。 – vipin