繁体   English   中英

将IONIC的JSON数据发送到Twilio PHP API

[英]Sending JSON data from IONIC to Twilio PHP API

尝试将IONIC应用程序中的JSON数据发送到服务器上的Twilio PHP API。

IONIC APP :单击提交按钮(sendSMS)后,我想发送短信到在表单输入(手机)中输入的任何手机号码:

sendSMS() {
this.submitAttempt = true;

  if(this.formGroup.valid){ 
      var link = 'https://mywebsite.com/send-sms.php';
      var myData = JSON.stringify({mobile: this.formGroup.controls[ 'mobile' ].value});

       this.http.post(link, myData)
      .subscribe(data => {
      this.data.response = data["_body"];
      });    
  }

PHP处理twilio请求 :我不知道我在下面的代码中遗漏了什么或做错了什么...我想向从IONIC应用接收的JSON数据(移动输入值)发送短信:

require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;

$postdata = file_get_contents("php://input");

$account_sid = "mysid";
$auth_token = "mytoken";
$twilio_phone_number = "mymobile";

$request = json_decode($postdata);
$mobile = $request->mobile;

$client = new Client($account_sid, $auth_token);

$client->messages->create(
   $mobile,
    array(
        "from" => $twilio_phone_number,
        "body" => "hello"
    )
);

原来这是一个HTTP问题。 对于其他尝试在其服务器上将Ionic与Twilio PHP脚本一起使用的人:

require_once 'vendor/twilio/sdk/Twilio/autoload.php'; 
use Twilio\Rest\Client;


if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    
}


if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers:        
        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

    exit(0);
}$postdata = file_get_contents("php://input");if (isset($postdata)) {

    $request = json_decode($postdata);
    $userMobile = $request->mobile;

if ($userMobile != "") {
    echo "Server returns: " . $userMobile;           


$account_sid = 'your-twilio-account-sid'; 
$auth_token = 'your-twilio-authtoken'; 
$client = new Client($account_sid, $auth_token); 

$client->messages->create(
        $userMobile,
        array(
            'From' => "YOUR TWILIO NUMBER",  
            'Body' => "MESSAGE YOU WANT TO SEND",   
        )
    );

echo "Sent message!";

} else {
    echo "Could not send SMS";
}   }

暂无
暂无

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

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