【问题标题】:Syntax error in PHP Twilio APIPHP Twilio API 中的语法错误
【发布时间】:2018-05-25 10:07:58
【问题描述】:

我按照website 上的说明进行操作,但我的 sendnotifications.php 文件中有错误,我没有收到发送确认消息,并且屏幕上未显示“消息已发送”和“消息未发送”消息.

SMS 一切正常,我在手机上收到。

这里是 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 Code 中的错误:

{
    "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

【问题讨论】:

  • 首先,在回显$return_json之前,确保内容类型标头设置为header('Content-Type: application/json);
  • 你真的在到处使用curly单引号‘...’吗? - 这不是正确的 PHP 语法,那些需要是 '
  • 您在 PHP 代码中的引号无效。
  • 智能行情,呃!好眼力。在 Mac 上,这些可以在您的键盘首选项中的某处禁用。你到底在用什么编码,MS Word? :-P
  • 哈哈,Visual Studio Code bur 我复制并粘贴了这段代码:D

标签: php html json twilio twilio-api


【解决方案1】:

该属性在 HTML 中为“sms_sent”,在 PHP 中为“sms_check”

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2016-10-23
    • 2011-12-07
    • 1970-01-01
    相关资源
    最近更新 更多