46elks / 46elks-getting-started

An introduction to the 46elks API with code examples
https://46elks.com/docs/
52 stars 26 forks source link

I get this error when I try send sms with 46elks Api php #34

Closed Hussein-ipc closed 4 years ago

Hussein-ipc commented 4 years ago

`<?php

include "../db.php";

//$_POST['command'] ;// $command = $_POST['command']; if ($command == "register_user") {//register user $mobile = $_POST['mobile']; $token = $_POST['token']; $sql = "SELECT * FROM tbl_user where mobile ={$mobile}"; $result = mysqli_query($connection, $sql); $num = mysqli_num_rows($result);

$user_id = 0;

if ($num == 0) {
    $query = "INSERT INTO tbl_user(mobile,tarikh,token)values('{$mobile}',now(),'{$token}')";
    $result = mysqli_query($connection, $query);
    $user_id = mysqli_insert_id($connection);
} else {
    $user = mysqli_fetch_assoc($result);
    $user_id = $user['id'];
}
//register finished

//delete all sms from database
$query = "delete from `tbl_sms_verify` where sms_user_id=$user_id";
$result = mysqli_query($connection, $query);

//insert sms to database
$code = rand(1000, 9999);
$query = "insert into `tbl_sms_verify`(sms_user_id,sms_code)values($user_id,'$code')";
$result = mysqli_query($connection, $query);

function sendSMS ($sms) {

    $username = 'u7dca0c584dfa6f53cc6fb607ac83030e';
    $password = '582593262C81212A6C06F314B30B43A2';

    $context = stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'header'  => "Authorization: Basic ".
                base64_encode($username.':'.$password). "\r\n".
                "Content-type: application/x-www-form-urlencoded\r\n",
            'content' => http_build_query($sms),
            'timeout' => 10
        )));

    $response = file_get_contents(
        'https://api.46elks.com/a1/SMS', false, $context );

    if (!strstr($http_response_header[0],"200 OK"))
        return $http_response_header[0];

    return $response;
}

$sms = array(

    'from' => 'PHPElk',
    'to' => '$mobile',
    'message' => '$code',

);
echo sendSMS ($sms) . "\n";

if ($command == "verify_code") {//

$mobile = $_POST['mobile'];
$code = $_POST['code'];

$query = "select * from tbl_user where mobile='{$mobile}'";

$result = mysqli_query($connection, $query);

$num = mysqli_num_rows($result);

if ($num == 0) {
    echo json_encode(array('result1' => "error1"));
} else {
    $user = mysqli_fetch_assoc($result);
    $user_id = $user['id'];

    $query = "select * from tbl_sms_verify where sms_user_id='{$user_id}' and sms_code='{$code}'";
    $result = mysqli_query($connection, $query);
    $num = mysqli_num_rows($result);

    if ($num == 0) {
        echo json_encode("error");
    } else {

        echo json_encode("ok" );
    }

}

}

?> `

I try pass two variable $mobile and $code into $ sms but I appea with Error. Can someone help why I get This Error.

Hussein-ipc commented 4 years ago

https://gist.github.com/Hussein-ipc/2213997b0ff2bcbdc06342d3bc76ff53

ghost commented 4 years ago

Hi Hussein!

Let's take it one step at a time!

It seems you've defined sendSMS like this function sendSMS ($variable, $variable, $variable) { ... } on line 43 in the file: sms_verify.php

When you run sendSMS, it's expecting you to enter 3 arguments, but you're giving it one.

Try commenting out the line 43 in sms_verify.php and then try again!