【问题标题】:how to use php class with html form如何在 html 表单中使用 php 类
【发布时间】:2017-02-12 06:45:21
【问题描述】:

好的,所以我创建了我希望与以下 php 类连接的 html 表单,但我不知道如何完成它,因为我以前从未使用过 php 类。

这是我的表格

<form action="sms.php" method="post">
<input type="text" name="uid" value=""/>
<input type="password" name="pwd" value=""/>
<input type="number" name="mobile" value=""/>
<textarea cols="30" rows="10" name="msg"></textarea>
<input type="submit" name="submit" value="Send"/>

这是我需要能够与上述 html 表单连接的 php 类

<?php
class WAY2SMSClient
{
    var $curl;
    var $timeout = 30;
    var $jsToken;
    var $way2smsHost;
    var $refurl;

    function login($username, $password)
    {
        $this->curl = curl_init();
        $uid = urlencode($username);
        $pwd = urlencode($password);
        // Go where the server takes you :P
        curl_setopt($this->curl, CURLOPT_URL, "http://way2sms.com");
        curl_setopt($this->curl, CURLOPT_HEADER, true);
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
        $a = curl_exec($this->curl);
        if (preg_match('#Location: (.*)#', $a, $r))
            $this->way2smsHost = trim($r[1]);
        // Setup for login
        curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "Login1.action");
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, "username=" . $uid . "&password=" . $pwd . "&button=Login");
        curl_setopt($this->curl, CURLOPT_COOKIESESSION, 1);
        curl_setopt($this->curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->curl, CURLOPT_MAXREDIRS, 20);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
        curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
        curl_setopt($this->curl, CURLOPT_REFERER, $this->way2smsHost);
        $text = curl_exec($this->curl);
        // Check if any error occured
        if (curl_errno($this->curl))
            return "access error : " . curl_error($this->curl);
        // Check for proper login
        $pos = stripos(curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL), "ebrdg.action");
        if ($pos === "FALSE" || $pos == 0 || $pos == "")
            return "invalid login";
        // Set the home page from where we can send message
        $this->refurl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
        $newurl = str_replace("ebrdg.action?id=", "main.action?section=s&Token=", $this->refurl);
        curl_setopt($this->curl, CURLOPT_URL, $newurl);
        // Extract the token from the URL
        $this->jstoken = substr($newurl, 50, -41);
        //Go to the homepage
        $text = curl_exec($this->curl);
        return true;
    }

    function send($phone, $msg)
    {
        $result = array();
        // Check the message
        if (trim($msg) == "" || strlen($msg) == 0)
            return "invalid message";
        // Take only the first 140 characters of the message
        $msg = substr($msg, 0, 140);
        // Store the numbers from the string to an array
        $pharr = explode(",", $phone);
        // Send SMS to each number
        foreach ($pharr as $p) {
            // Check the mobile number
            if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false) {
                $result[] = array('phone' => $p, 'msg' => $msg, 'result' => "invalid number");
                continue;
            }
            // Setup to send SMS
            curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . 'smstoss.action');
            curl_setopt($this->curl, CURLOPT_REFERER, curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL));
            curl_setopt($this->curl, CURLOPT_POST, 1);
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, "ssaction=ss&Token=" . $this->jstoken . "&mobile=" . $p . "&message=" . $msg . "&button=Login");
            $contents = curl_exec($this->curl);
            //Check Message Status
            $pos = strpos($contents, 'Message has been submitted successfully');
            $res = ($pos !== false) ? true : false;
            $result[] = array('phone' => $p, 'msg' => $msg, 'result' => $res);
        }
        return $result;
    }
    /**
     * logout of current session.
     */
    function logout()
    {
        curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "LogOut");
        curl_setopt($this->curl, CURLOPT_REFERER, $this->refurl);
        $text = curl_exec($this->curl);
        curl_close($this->curl);
    }
}

function sendWay2SMS($uid, $pwd, $phone, $msg)
{
    $client = new WAY2SMSClient();
    $client->login($uid, $pwd);
    $result = $client->send($phone, $msg);
    $client->logout();
    return $result;
}
?>

感谢您的时间和帮助

【问题讨论】:

    标签: php html curl


    【解决方案1】:

    sms.php

    <?php
    //include WAY2SMSClient file
    include_once 'WAY2SMSClient.php';
    
    $uid = $_POST['uid'];
    $pwd = $_POST['pwd'];
    $phone = $_POST['mobile'];
    $msg = $_POST['msg'];
    $result = sendWay2SMS($uid, $pwd, $phone, $msg);
    ?>
    

    【讨论】:

    • include_once 的参数必须是带引号的字符串。
    • 欣赏它@B.Desai 但猜测上面的代码不起作用,因为没有发送短信
    【解决方案2】:

    您有一个名为 WAY2SMSCLIENT.php 的文件,并且您将表单数据发布到 sms.php。 因此,您必须从您的 sms.php 创建一个 WAY2SMSCLIENT 类的对象,然后在该对象上您可以调用方法,将所需的数据作为参数传递给它们。 很抱歉,因为我现在无法发布代码,因为我正在使用电话并且正在旅行。但是,如果对您来说仍然很难,请告诉我,我会尽力帮助您。

    【讨论】:

    • 嗨@simplysaif 将不胜感激。只要你也可以分享你的代码。
    • @B.desai 发布的分析器必须工作正常。你需要调试你的代码。如果没有发送短信,您应该发布您收到的错误。我会发布相同的代码。试试这个 var_dump( $client->login($uid, $pwd));并检查您的登录功能返回什么值'
    • 是的,它正在工作,但如果 $result 有错误,我不能得到响应,如果没有错误,则消息不发送,然后消息发送
    • 如果从您的登录方法登录成功,则返回 true,否则返回错误消息。而不是从登录方法返回真正的返回“成功”,然后在 sendWay2SMS 内部检查这样的 $login = $client->login($uid, $pwd); if($login == 'success'){ //发送消息} else{//显示错误消息}
    • 会试一试然后回来。欣赏它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多