trevrobwhite
trevrobwhite

Reputation: 453

Connecting to Twilio SMS via a Proxy in PHP

I'm testing Twilio to use as our SMS solution however I'm having issues getting it to work behind our proxy server.

I've tried:

$twiliohttp = new Services_Twilio_TinyHttp(
        "https://api.twilio.com",
        array("curlopts" => array(
    CURLOPT_USERAGENT => self::USER_AGENT,
    CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
    CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
    CURLOPT_PROXY => '3.X.X.X:9400',
    ))
);
$client = new Services_Twilio($account_sid, $auth_token, null, $twiliohttp );


$message = $client->account->messages->sendMessage(
  '+441432XXXX31', // From a Twilio number in your account
  '+44776XXXX712', // Text any number
  "Hello monkey!"
);

I then get the error: Fatal error: Cannot access self:: when no class scope is active in /var/www/twiliosms.php on line 16

So I modified the Twilio.php file modifying the curlopts array to add:

CURLOPT_PROXY => '3.X.X.X:9400',

and calling Twilio with:

$client = new Services_Twilio($account_sid, $auth_token );
$message = $client->account->messages->sendMessage(
  '+4414XXXXXXX1', // From a Twilio number in your account
  '+4477XXXXXXX2', // Text any number
  "Hello monkey!"
);

But I then get the error:

Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'Could not decode response body as JSON. This likely indicates a 500 server error' in /var/www/GE/includes/SMS/Twilio.php:288
Stack trace:
#0 /var/www/GE/includes/SMS/Twilio.php(181): Base_Services_Twilio->_processResponse(Array)
#1 /var/www/GE/includes/SMS/Twilio/ListResource.php(92): Base_Services_Twilio->createData('/2010-04-01/Acc...', Array)
#2 /var/www/GE/includes/SMS/Twilio/Rest/Messages.php(24): Services_Twilio_ListResource->_create(Array)
#3 /var/www/GE/includes/SMS/Twilio/Rest/Messages.php(71): Services_Twilio_Rest_Messages->create(Array)
#4 /var/www/GE/twiliosms.php(35): Services_Twilio_Rest_Messages->sendMessage('+441432233131', '+447766205712', 'Hello monkey!')
#5 {main}
  thrown in /var/www/GE/includes/SMS/Twilio.php on line 288

Any ideas how to make this solution work through a proxy server that doesn't allow inbound connections?

Thanks in advance.

Upvotes: 0

Views: 651

Answers (2)

rickyrobinett
rickyrobinett

Reputation: 1244

Ricky from Twilio here.

Although I couldn't test with your exact proxy setup I think the first solution you tried will work if you hardcode the user agent. For example:

$twiliohttp = new Services_Twilio_TinyHttp(
        "https://api.twilio.com",
        array("curlopts" => array(
    CURLOPT_USERAGENT => "Twilio Proxy/1.0",
    CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
    CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
    CURLOPT_PROXY => '3.X.X.X:9400',
    ))
);

You also may need to make the modification to the TinyHttp library shown here.

Upvotes: 2

trevrobwhite
trevrobwhite

Reputation: 453

Many thanks to RickyRobinett this is how to resolve the issue, I post this answer with the full solution so people don't have to trove through the badly formatted comments.

Update TinyHttp in line with: https://github.com/camuthig/twilio-php/commit/20d4f3c4479c93894866f498e89a0f13cf16d6bf

$twiliohttp = new Services_Twilio_TinyHttp(
        "https://api.twilio.com",
        array("curlopts" => array(
    CURLOPT_USERAGENT => "Twilio Proxy/1.0",
    CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
    CURLOPT_CAINFO => 'includes/SMS/cacert.pem',
    CURLOPT_PROXY => 'X.X.X.X:9400',
    ))
);

$client = new Services_Twilio($account_sid, $auth_token, null, $twiliohttp );

If you don't have the cacert.pem then checkout this post https://stackoverflow.com/a/31297747/1697288

Upvotes: 1

Related Questions