简体   繁体   English

我的jQuery和twilio代码有什么问题?

[英]What is wrong with my jQuery and twilio Code?

I went to these two ( 1 , 2 ) site for one of my projects. 我去了这两个( 12 )网站我的项目之一。
I'm a noob at PHP. 我是PHP的菜鸟。 So I'm not really sure how to make this work. 所以我不确定如何使这项工作。
I once got a SSL certificate error while trying to test it. 我在尝试测试时遇到了SSL证书错误。 My SMS are not getting sent. 我的短信没有收到。
I don't get any kind of reply or alert or something when I'm testing it with my HTML. 当我用HTML测试它时,我没有得到任何回复或警告或其他东西。
Please help me if there's any twilio and PHP expert are out there. 如果有任何twilio和PHP专家在那里,请帮助我。 this the form I'm using in HTML 这是我在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>

My script 我的剧本

$("#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");
});

and here is my sendnotifications.php file 这是我的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;
?>  

Please someone tell me what do I need to change to get my site working. 请有人告诉我,为了让我的网站正常运行,我需要做些什么改变。
I have changed the $http in sendnotifications.php after going to the github faq of twilio to prevent the ssl certificate error. 我在转到twilio的github faq之后更改了sendnotifications.php中的$ http以防止ssl证书错误。 What else do I need to do?? 还有什么我需要做的?

Edit: I have solved the problem using the below code. 编辑:我已使用以下代码解决了问题。

    <?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;
?>

But I'm now getting an error in my server.. 但我现在在服务器上收到错误..
The SMS is getting sent but this error shows up.. SMS正在发送,但此错误显示..

Error: Could not decode response body as JSON. 错误:无法将响应正文解码为JSON。 This likely indicates a 500 server error 这可能表示500服务器错误

Does anyone know any way of solving this problem.. My website is hosted on altervista. 有谁知道解决这个问题的方法..我的网站托管在altervista上。

Edit I have solved the 500 error 编辑我已经解决了500错误

By replacing the tinyHttp.php file inside "Services/Twilio"with a edited tinyhttp.php file from github.. The link of the edited github file is in the comment section.. 通过使用github编辑的tinyhttp.php文件替换“Services / Twilio”中的tinyHttp.php文件。编辑的github文件的链接位于注释部分。

Current Issue: 目前的问题:

Try this 尝试这个

require_once "Services/Twilio.php";
$AccountSid = "ACexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
$AuthToken = "fafxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$client = new Services_Twilio($$AccountSid, $AuthToken);

$phone=$_POST["phoneNumber"];

try{
$client->account->messages->sendMessage("+17xxxxxx", $phone, "This code is for testing an android app website!! Get our app now: http://bit.ly/ourapp");
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

Past Issue: 过去的问题:

It looks like you weren't consistent with your variables. 看起来你与你的变量不一致。 $sid and $token might not be set, you used $AccountSid and $AuthToken to set your variables. 可能未设置$sid$token ,您使用$AccountSid$AuthToken来设置变量。 Try this 尝试这个

$client = new Services_Twilio($AccountSid, $AuthToken, "2010-04-01", $http);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM