【发布时间】:2015-06-20 15:35:36
【问题描述】:
我去了这两个(1,2)站点,以完成我的一个项目。
我是 PHP 的菜鸟。所以我不太确定如何完成这项工作。
我曾经在尝试对其进行测试时遇到 SSL 证书错误。我的短信没有收到。
当我用我的 HTML 对其进行测试时,我没有收到任何形式的回复或警告或其他东西。
如果有任何twilio and PHP 专家,请帮助我。
这是我在 HTML 中使用的表单
<form id="frm" name="frm">
<div class="form-group">
<label for="tel">Phone:</label>
<input type="tel" class="form-control" id="phoneNumber" name="phoneNumber" required>
</div>
<button class="btn" type="submit" id="submit">Submit</button>
</form>
我的脚本
$("#frm").submit(function(e){
e.preventDefault();
$.post("sendnotifications.php", $("#frm").serialize(),
function(data){
if(data.sms_sent == 'OK'){
alert("Message Sent");
} else {
alert("Message Not Sent");
}
}, "json");
});
这是我的 sendnotifications.php 文件
<?php/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* - Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* - Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
* - Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACexxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "fafyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
// Step 3: instantiate a new Twilio Rest Client
$http = new Services_Twilio_TinyHttp(
'https://api.twilio.com',
array('curlopts' => array(
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
))
);
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
// Step 4: Get phone number from the test-sms form
$phone=$_POST["phoneNumber"];
// Step 5: Create SMS
$sms = $client->account->sms_messages->create(
// Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"717-xxx-xxxx",
// the number we are sending to - Any phone number
$phone,
// the sms body
"Get our app now: http://example.com/ourapp"
);
// Display a confirmation message on the screen
$sms_check='OK'; //Use Twilio's callback here
$return_json = '{"sms_sent":"' . $email_check . '"}';
echo $return_json;
?>
请有人告诉我我需要进行哪些更改才能使我的网站正常运行。
我在去 twilio 的 github faq 之后更改了 sendnotifications.php 中的 $http 以防止 ssl 证书错误。我还需要做什么??
编辑:我已经使用下面的代码解决了这个问题。
<?php
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require_once "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "04dxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Twilio REST API version
$version = "2010-04-01";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken, $version);
// Step 4: Get phone number from the test-sms form
$phone=$_POST["phoneNumber"];
try{
$message = $client->account->messages->create(array(
"From" => "+1xxxxxxxxxx",
"To" => $phone,
"Body" => "This code is for testing!",
));
$sms_check='OK';
}
catch (Exception $e) {
$sms_check = 'Error';
}
$return_json = '{"sms_sent":"' . $sms_check . '"}';
echo $return_json;
?>
但我的服务器现在出现错误..
SMS 正在发送,但出现此错误..
错误:无法将响应正文解码为 JSON。这可能表示 500 服务器错误
有谁知道解决这个问题的任何方法.. 我的网站托管在 altervista 上。
编辑我已经解决了 500 错误
通过将 "Services/Twilio" 中的 tinyHttp.php 文件替换为来自 github 的已编辑 tinyhttp.php 文件.. 已编辑 github 文件的链接在评论部分中..
【问题讨论】:
-
你能发布你遇到的任何错误吗,同时查看 apache 日志
-
我已经编辑了我的问题...如果你能帮助我,请告诉我。 :)
-
我已经更新了答案
标签: javascript php html twilio twilio-php