2016-09-15 59 views
1

我想在我的php項目中使用Mailgun。 我已經安裝了所有必要的組件:Guzzle Http找不到mailgun

  • 作曲
  • 狂飲6

這是我的PHP代碼來測試電子郵件服務:

require 'vendor/autoload.php'; 
ini_set('display_errors', 'On'); 
$client = new \Http\Adapter\Guzzle6\Client(); 

define('MAILGUN_KEY', 'key-xxxxxxxxxx'); 
define('MAILGUN_DOMAIN', 'my-domain.nl'); 

$mailgun = new \Mailgun\Mailgun(MAILGUN_KEY, $client); 

$mailgun->sendMessage(MAILGUN_DOMAIN, [ 
      'from'  => '[email protected]', 
      'to'  => '[email protected]', 
      'subject' => 'This is a test e-mail', 
      'html'  => " 
       Hello,</br></br> 
       This is a test." 
     ]); 

它仍然引發以下錯誤:

Fatal error: Class 'Http\Adapter\Guzzle6\Client' not found 

爲什麼在composer.json中安裝並需要Guzzle6時仍然無法找到它?

編輯:

也許一些重要的信息,我已經安裝了作曲家和狂飲在/ usr/local/lib目錄,並使其全局可用。我應該這樣做還是將它們安裝在我的域的根文件夾中?

+0

不應該是'$ client = new \ GuzzleHttp \ Client();' – Farkie

+0

這只是返回一個不同的錯誤:'致命錯誤:Class'GuzzleHttp \ Client''代替另一個... so沒有運氣,但.. – user3055582

+0

你是什麼意思的「使其全球可用」? –

回答

1

我找到了問題的根源:

顯然我需要在我的領域,而不是全球性的根文件夾中安裝作曲家和mailgun依賴。

我改變了路徑狂飲這一點,因爲我發現這個在論壇上:

$客戶端=新\ GuzzleHttp \客戶端();

這給了我這個錯誤:

Catchable fatal error: Argument 2 passed to Mailgun\Mailgun::__construct() must be an instance of Http\Client\HttpClient, instance of GuzzleHttp\Client 

其中我固定在終端中運行以下命令:

php composer.phar require php-http/guzzle6-adapter:^1.0 

和改變路徑返回到原來的路徑:

$client = new \Http\Adapter\Guzzle6\Client(); 

所以現在mailgun使用這個代碼很好用:

require 'vendor/autoload.php'; 
ini_set('display_errors', 'On'); 
$client = new \Http\Adapter\Guzzle6\Client(); 

define('MAILGUN_KEY', 'key-xxxxxxxxxx'); 
define('MAILGUN_DOMAIN', 'my-domain.nl'); 

$mailgun = new \Mailgun\Mailgun(MAILGUN_KEY, $client); 

$mailgun->sendMessage(MAILGUN_DOMAIN, [ 
     'from'  => '[email protected]', 
     'to'  => '[email protected]', 
     'subject' => 'This is a test e-mail', 
     'html'  => " 
      Hello,</br></br> 
      This is a test." 
    ]);