如何使用带有html格式的php类

时间:2017-02-12 06:45:21

标签: php html curl

好吧所以我创建了html表单,我想连接下面的php类,但我对如何完成它一无所知,因为我以前从未在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;
}
?>

感谢您的时间和帮助

2 个答案:

答案 0 :(得分:0)

<强> 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);
?>

答案 1 :(得分:0)

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