通过PHP中的代理连接到Twilio SMS

时间:2015-10-20 13:38:03

标签: twilio twilio-php

我正在测试Twilio作为我们的SMS解决方案,但是我遇到了在代理服务器后面工作的问题。

我试过了:

$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!"
);

然后我得到错误:致命错误:当第16行的/var/www/twiliosms.php中没有类活动时,无法访问self ::

所以我修改了修改curlopts数组的Twilio.php文件来添加:

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

并致电Twilio:

$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!"
);

但是我得到了错误:

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

如何使这个解决方案通过不允许入站连接的代理服务器工作?

提前致谢。

2 个答案:

答案 0 :(得分:2)

来自Twilio的Ricky。

虽然我无法使用您的确切代理设置进行测试,但我认为如果您对用户代理进行硬编码,您尝试的第一个解决方案将会起作用。例如:

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

您还可能需要对显示为here的TinyHttp库进行修改。

答案 1 :(得分:1)

非常感谢RickyRobinett这是如何解决这个问题的,我用完整的解决方案发布了这个答案,所以人们不必通过格式错误的评论。

根据https://github.com/camuthig/twilio-php/commit/20d4f3c4479c93894866f498e89a0f13cf16d6bf

更新TinyHttp
$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 );

如果您没有cacert.pem,请查看此帖子https://stackoverflow.com/a/31297747/1697288