如何使用API 发送自动SMS?

问题描述 投票:-2回答:1

我有这种情况可以安排我的短信。 (错误),但我使用的短信是POST方法,我需要将其自动化。并发送短信到当前ID号。我不知道如何获取当前ID的编号并发送消息。这是我的代码:

$duedate = "";
$date_now = date("Y-m-d");
date_default_timezone_set('Asia/Manila');
$date_now = strtotime($date_now);
$duedate = strtotime($duedate);

if ($duedate <= $date_now) {
    function itexmo($number, $message, $apicode) {
        $url = 'https://www.itexmo.com/php_api/api.php';
        $itexmo = array('1' => $number, '2' => $message, '3' => $apicode);
        $param = array(
            'http' => array(
                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                'method' => 'POST',
                'content' => http_build_query($itexmo),
            ),
        );
        $context = stream_context_create($param);
        return file_get_contents($url, false, $context);
    }

    if ($_POST) {
        $number = $number;
        $name = $name;
        $api = "API";
        $text = $name." : ".$number;

        if (!empty($_POST['name']) && ($_POST['number']) && ($_POST['msg'])) {
            $result = itexmo($number, $text, $api);
            if ($result == "") {
                echo "iTexMo: No response from server!!!
Please check the METHOD used (CURL or CURL-LESS). If you are using CURL then try CURL-LESS and vice versa.  
Please CONTACT US for help. ";
            } elseif ($result == 0) {
                echo "Message Sent!";
            } else {
                echo "Error Num ". $result . " was encountered!";
            }
        }
    }
}
php sms
1个回答
0
投票

使用PHP cURL比stream_context_create和file_get_contents更好。看看下面的代码,它应该对您有帮助。

function itexmo($number,$message,$apicode) 
{
    $url = 'https://www.itexmo.com/php_api/api.php';        
    $postValues =  http_build_query(['1' => $number, '2' => $message, '3' => $apicode])

        $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postValues);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "cache-control: no-cache",
                "content-type: application/x-www-form-urlencoded"
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
    curl_close ($ch);

    return $response;
}

您可以了解更多PHP cURL

© www.soinside.com 2019 - 2024. All rights reserved.