【问题标题】:fatal error while trying to send message using twilio Api phpSDK尝试使用 twilio Api phpSDK 发送消息时出现致命错误
【发布时间】:2017-04-20 17:53:12
【问题描述】:

当我使用 twilio 时出现以下错误:

PHP 可捕获的致命错误:参数 2 传递给 Twilio\Rest\Api\V2010\Account\MessageInstance::__construct() 必须是 类型数组,给定null,调用 /data/home/changliang/twilio/twilio-php-master/Twilio/Rest/Api/V2010/Account/MessageList.php 在第 69 行并在 /data/home/changliang/twilio/twilio-php-master/Twilio/Rest/Api/V2010/Account/MessageInstance.php 第 52 行

这是我的代码。

require_once("/twilio/twilio-php-master/Twilio/autoload.php");
use Twilio\Rest\Client;
$to = '+12022022022'
$content = 'hello';
$sid = 'XXXXXXX'; 
$token = 'XXXXXXXX'; 
$client = new Client($sid, $token);
$sms = $client->account->messages->create(  
    $to,
    array(
        'from' => '+12346788xx',
        'body' => $content,
    )  
);

【问题讨论】:

    标签: php twilio fatal-error


    【解决方案1】:

    有同样的错误。您的请求是否可能通过公司代理服务器发出?这就是这里的问题。此处的代理服务器在响应头中添加了一个额外的 HTTP 头,因此 CurlClient 无法正确解析响应正文:

    HTTP/1.1 200 连接建立

    我通过在第 37 行附近的 CurlClient 类中添加一个额外的标题来修复它:

    原文:

    list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
                               ? array($parts[1], $parts[2])
                               : array($parts[0], $parts[1]);
    

    新:

    list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue'
                       || $parts[0] == 'HTTP/1.1 200 Connection established')
                               ? array($parts[1], $parts[2])
                               : array($parts[0], $parts[1]);
    

    【讨论】:

    • 工作没有任何问题。 curl客户端放在目录:twilio_outbound_phone_call/twilio-php-master/Twilio/Http
    【解决方案2】:

    Bastian 的修复对我有用,但看起来 sdk 可能在他回复后发生了一些变化。对我来说,它最初看起来像:

    list($head, $body) = (
        \preg_match('/\AHTTP\/1.\d 100 Continue\Z/', $parts[0])
        || \preg_match('/\AHTTP\/1.\d 200 Connection established\Z/', $parts[0])
        || \preg_match('/\AHTTP\/1.\d 200 Tunnel established\Z/', $parts[0])
    )           ? array($parts[1], $parts[2])
                : array($parts[0], $parts[1]);
    

    我的代理返回的标题是:

    HTTP/1.1 200 OK
    Connection: Keep-Alive
    

    对我来说,解决方法是添加一个正则表达式来捕获基本上 200 个标头响应:

    list($head, $body) = (
        \preg_match('/\AHTTP\/1.\d 100 Continue\Z/', $parts[0])
        || \preg_match('/\AHTTP\/1.\d 200 Connection established\Z/', $parts[0])
        || \preg_match('/\AHTTP\/1.\d 200 Tunnel established\Z/', $parts[0])
        || \preg_match('/\AHTTP\/1.\d 200 .*/', $parts[0])
    )           ? array($parts[1], $parts[2])
                : array($parts[0], $parts[1]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2011-04-05
      • 2021-07-26
      • 2020-12-05
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多