PHP Twilio API中的语法错误

时间:2018-05-25 10:07:59

标签: php html json twilio twilio-api

我按照此website的说明操作,我的sendnotifications.php文件中有错误,我没有收到发送确认和消息"已发送消息"和"消息未发送"不要在我的屏幕上显示。

一切都适用于短信,我通过手机接收。

这里是PHP文件:

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = ‘MyAccountSID’;
$token = ‘MyToken’;
$client = new Client($sid, $token);
$phone=$_POST["phone"];

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    $phone,
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => ‘MyTwilioNumber’,
        // the body of the text message you'd like to send
        'body' => « MyMessage »
    )
);

// Display a confirmation message on the screen
$sms_check=’OK’; //Use Twilio’s callback here
$return_json = ‘{"sms_sent":"’ . $email_check . ‘"}’;

echo $return_json;

?>

这是我的HTML表单:

<!doctype html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">// <![CDATA[///////////Javascript
    $(function(){
    $("#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");
    });

    });
    // ]]>
    </script>
    </head>

<html>

<form id="frm" name="frm">
<input type="hidden" name="ajax" value="1" />
<input type="phone" name="phone" />
<button type="submit">Get Link</button>
<div class="error" style="display: none;"></div>
</form>

</html>

这是Visual Studio代码中的错误:

{
    "resource": "/Users/Joris/Desktop/Test envoi sms accueil/sendnotifications.php",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "undefined",
    "severity": 8,
    "message": "syntax error, unexpected '{'",
    "startLineNumber": 31,
    "startColumn": 1,
    "endLineNumber": 31,
    "endColumn": 1.7976931348623157e+308
}

这是Chrome上的错误:

Uncaught TypeError: Cannot read property 'sms_sent' of null
    at Object.success (source.html:10)
    at b (jquery.min.js:124)
    at XMLHttpRequest.x.onreadystatechange (jquery.min.js:129)
(anonymous) @ source.html:10
b @ jquery.min.js:124
x.onreadystatechange @ jquery.min.js:129
XMLHttpRequest.send (async)
ajax @ jquery.min.js:130
post @ jquery.min.js:122
(anonymous) @ source.html:8
handle @ jquery.min.js:55
o @ jquery.min.js:49

1 个答案:

答案 0 :(得分:0)

该属性在HTML中为'sms_sent',在PHP中为'sms_check'