0

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.

trevrobwhite
  • 443
  • 1
  • 7
  • 22

2 Answers2

2

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.

rickyrobinett
  • 1,224
  • 10
  • 13
  • I now get: 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 – trevrobwhite Oct 20 '15 at 15:46
  • In Twilio.php on line 288 trying dumping the response you're getting. Looking at this response may provide more insight into why we're unable to parse the request as json: private function _processResponse($response) { var_dump($response); – rickyrobinett Oct 20 '15 at 15:58
  • It's returning a 404 error, however I can curl to that proxy without issue, is there any chance you can do support session? Our proxy will only allow https if that's an issue string(581) "HTTP/1.1 404 NOT FOUND Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS Access-Control-Allow-Origin: * – trevrobwhite Oct 20 '15 at 16:08
  • Definitely looks like this may be easier to debug together. Wanna drop me an e-mail and we'll get something set up - ricky@twilio.com? – rickyrobinett Oct 20 '15 at 17:21
1

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

Community
  • 1
  • 1
trevrobwhite
  • 443
  • 1
  • 7
  • 22