使用php通过API集成发送短信

时间:2019-12-29 10:29:22

标签: php api integration

以下代码包含HTML和PHP代码。我将SMS API与HTML集成在一起,但是它不起作用,它不会显示任何错误消息。我需要您的帮助。我想通过此API成功发送SMS。HTML部分包含SMS发送字段的结构,而PHP实际上包含API代码。

<pre>
    <!DOCTYPE html>
    <html>
    <head>
        <title>
            Sending SMS using PHP
        </title>
    </head>
    <body>
        <form method="post">
            <br><br>
            <label>Mobile Number:</label>
            <input type="text" name="num">
            <br><br>
            <label>Country Code:</label>
            <select name="code">
                <option value="">Select Here</option>
                <option value="92">Pakistan - +92</option>
                <option value="91">India - +91</option>
                <option value="1">USA - +1</option>
            </select>
            <br><br>
            <label>Enter Message</label>
            <input type="text" name="message">
            <input type="submit" name="submit">


        </form>

    </body>
    </html>

    <?php

        if (isset($_POST["submit"])) {


        // Authorisation details.
        $username = "example@gmail.com";
        $hash = "some_hash";

        // Config variables. Consult http://api.txtlocal.com/docs for more info.
        $test = "0";

        // Data for text message. This is the text message data.
        $sender = "API Test"; // This is who the message appears to be from.
        $numbers = $_POST["code"] . $_POST["num"]; // A single number or a comma-seperated list of numbers
        $message = $_POST["message"];
        // 612 chars or less
        // A single number or a comma-seperated list of numbers
        $message = urlencode($message);
        $data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
        $ch = curl_init('http://api.txtlocal.com/send/?');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch); // This is the result from the API
        curl_close($ch);

        if (!$result) {
            ?>
            <script>
                alert('Message Not Sent...!')
            </script>
        <?php}
        else{
             #print result
        echo $result;
        ?>
        <script>
            alert('Message Sent Sucessfully...!')
        </script>
            <?php
    }  
    }
    ?>
    </pre>

0 个答案:

没有答案