InvalidArgumentException:无效资源类型:boolean


Postmark/Guzzle InvalidArgumentException: Invalid resource type: boolean

我一直收到一些通过邮戳发送的邮件的InvalidArgumentException: Invalid resource type: boolean。有些人在工作,有些人则没有。我看不出正常的邮件和有错误的邮件有什么不同。它们用相同的参数运行相同的代码。

这是我传递到邮戳的内容:

$client = new PostmarkClient($config->postmark->token);
$sendResult = $client->sendEmail(
  $config->postmark->sender,
  $recipient,
  $subject,
  $html_body,
  $text_body,
  null, true, null, null, null, null, $attachments
);

下面是调用栈:

vendor/guzzlehttp/streams/src/Stream.php in factory at line 85
throw new 'InvalidArgumentException('Invalid resource type: ' . $type);
vendor/guzzlehttp/guzzle/src/Message/MessageFactory.php in applyOptions at line 345
$request->setBody(Stream::factory(json_encode($value)));
vendor/guzzlehttp/guzzle/src/Message/MessageFactory.php in createRequest at line 98
$this->applyOptions($request, $options);
vendor/guzzlehttp/guzzle/src/Client.php in createRequest at line 120
return $this->messageFactory->createRequest($method, $url, $options);
vendor/wildbit/postmark-php/src/Postmark/PostmarkClientBase.php in processRestRequest at line 101
$request = $client->createRequest($method, $url, $options);
vendor/wildbit/postmark-php/src/Postmark/PostmarkClient.php in sendEmail at line 61
return new DynamicResponseModel($this->processRestRequest('POST', '/email', $body));
library/send_mail.php in send_mail at line 86

我在谷歌上搜索了一段时间,没有找到任何能解释这个问题的东西。

我看过这里:https://laracasts.com/discuss/channels/general-discussion/mandrill-trouble-invalid-resource-type-boolean在这里:https://laracasts.com/forum/?p=2325-problems-using-mandrill/0但这两个都有Laravel+Mandrill的问题,但我使用的是邮戳,没有框架。然而,我们确实有"狂饮"的共同点。这两个帖子都说他们通过更新或重新安装他们的Guzzle来解决这个问题。

我似乎正在运行Guzzle的"工作"版本。GitHub Issue #628 for Guzzle似乎正是我所看到的,但这个问题说它在4.0.2中修复了,我在Guzzle 5.3上:

从我的composer.lock在服务器上:

{
  "name": "guzzlehttp/guzzle",
  "version": "5.3.0"
},
...
{
  "name": "guzzlehttp/streams",
  "version": "3.0.0"
},
...
{
  "name": "wildbit/postmark-php",
  "version": "dev-master",
  "source": {
      "type": "git",
      "url": "https://github.com/wildbit/postmark-php",
      "reference": "2eabc627c3f2a693b986e51d2f892cc2e2d065b3"
  },
  "require": {
    "guzzlehttp/guzzle": "~5.1",
    "php": ">=5.4.0"
  },
}

任何想法吗?

当您的错误不是立即明显时,那么调用堆栈通常会揭示出问题所在。这也不例外,我应该早点注意到的。

调用堆栈中的倒数第二项是出错的地方。

$request->setBody(Stream::factory(json_encode($value)));

这里,json_encode($value)返回一个布尔值。使用json_last_error(),我能够确定问题是与混合字符编码(JSON_ERROR_UTF8)。

在用户输入的某个地方有用户提供的数据,这些数据混合了UTF-8和ISO-8895-1。由于这是不一致的,有些邮件可以正常工作,而其他邮件则失败。

我最后强迫我的$html_body$text_body是UTF-8,事情开始工作。