2

When I use twilio I get following error:

PHP Catchable fatal error: Argument 2 passed to Twilio\Rest\Api\V2010\Account\MessageInstance::__construct() must be of the type array, null given, called in /data/home/changliang/twilio/twilio-php-master/Twilio/Rest/Api/V2010/Account/MessageList.php on line 69 and defined in /data/home/changliang/twilio/twilio-php-master/Twilio/Rest/Api/V2010/Account/MessageInstance.php on line 52

this is my code.

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,
    )  
);

2 Answers 2

3

Had the same error. Is your request maybe made through a corporate proxy server? That was the issue here. The proxy server here adds an additional HTTP header to the response header and because of this the CurlClient does not correctly parses the response body:

HTTP/1.1 200 Connection established

I got it fixed by adding an additional header to skip in CurlClient class around line 37:

Original:

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

New:

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

works without any problems. Curl client is placed in directory: twilio_outbound_phone_call/twilio-php-master/Twilio/Http
1

Bastian's fix worked for me, but it looks like the sdk might have changed a little since his response. For me it looked originally like:

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]);

and the header my proxy was returning was:

HTTP/1.1 200 OK
Connection: Keep-Alive

the fix for me was to add a regex to catch basically 200 header response:

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.