短信api出错

时间:2014-04-18 06:18:51

标签: php

我想通过php代码发送短信我正在使用的网站是160 by2或way2sms,我从http://www.pointerbrain.com/php-script-send-sms-using-way2sms-or-160by2-gateway/#.U1CzT_mSw9R获得的同一个api 执行时的api显示如下错误。请帮忙

// sms.php

<?php
//Use gateway of way2sms.com or 160by2.com
error_reporting(E_ALL);
ob_implicit_flush(true);

//If you get Fatal error: Call to undefined function curl_init() , Then you need to enable the curl extension in php.ini

include_once "class.curl.php";
include_once "class.sms.php";

$smsapp=new sms();
$smsapp->setGateway('160BY2'); // you can set gateway to be '160by2' to use your 160by2 account or way2sms to use your way2sms.com account;

$mobile_no = '********'; //Mobile no (Username)
$pass = '*****'; //Password
$send_to_no = '*******'; //Your friend's mobile no
$msg = 'Hi Dear, How r u';

echo "Logging in  ... ";
$smsapp->login($mobile_no,$pass);

echo "Sending SMS ... ";
$result=$smsapp->send($send_to_no,$msg);

if($result=='true')
{
    echo "Message sent";
}
else
{   
    echo "Error encountered : ".$smsapp->getLastError();
}

?>

// class.curl.php

<?php

class cURL {
    var $headers;
    var $user_agent;
    var $compression;
    var $cookie_file;
    var $proxy;
    function cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') {
        $this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
        $this->headers[] = 'Connection: Keep-Alive';
        $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
        $this->headers[] = 'Accept-Language: en-us,en;q=0.5';
        $this->headers[] = 'Accept-Encoding gzip,deflate';
        $this->headers[] = 'Keep-Alive: 300';
        $this->headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
        $this->user_agent = 'iPhone 4.0';
        $this->compression=$compression;
        $this->proxy=$proxy;
        $this->cookies=$cookies;
        if ($this->cookies == TRUE) $this->cookie($cookie);
    }

    function setUserAgent($ua)
    {
    }

    function setProxy($proxy)
    {
        $this->proxy=$proxy;
    }

    function cookie($cookie_file) {
        if (file_exists($cookie_file)) {
            $this->cookie_file=$cookie_file;
        } else {
            fopen($cookie_file,'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');
            $this->cookie_file=$cookie_file;
            fclose($this->cookie_file);
        }
    }

    function get($url) {
        $process = curl_init($url);
        curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($process, CURLOPT_HEADER, 0);
        curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
        if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
        if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
        curl_setopt($process,CURLOPT_ENCODING , $this->compression);
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
        curl_setopt($process, CURLOPT_PROXY, $this->proxy);
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
        $return = curl_exec($process);
        curl_close($process);
        return $return;
    }

    function post($url,$data,$referer=false) {
        $process = curl_init($url);
        curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($process, CURLOPT_HEADER, 1);
        curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
        if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
        if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
        curl_setopt($process, CURLOPT_ENCODING , $this->compression);
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
        curl_setopt($process, CURLOPT_PROXY, $this->proxy);
        curl_setopt($process, CURLOPT_POSTFIELDS, $data);
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
        if($referer)
        {
            curl_setopt($process, CURLOPT_REFERER, $referer);
        }
        curl_setopt($process, CURLOPT_POST, 1);
        curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 2); 
        $return = curl_exec($process);
        curl_close($process);
        return $return;
    }

    function error($error) {
        echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
        die;
    }
}
?>

// class.sms.php

<?php
class sms
{
    var $username;
    var $password;
    var $curl;
    var $server;
    var $loginDone;
    var $debugMode;
    var $data;
    var $error;
    public function __construct()
    {
        $this->curl=new cURL();
    //  $this->curl->setProxy("");
        $this->loginDone=false;
        $this->debugMode=false;
        $this->data=array();
    }
    public function setGateway($serverName)
    {
        switch($serverName)
        {
            case '160by2':
            $this->server='160by2';
            break;

            case 'way2sms':
            $this->server='way2sms';
            break;

            case 'airtel':
            $this->server='airtel';
            break;

            default :
            $this->server='160by2';

        }
    }
    public function login($username,$password)
    {
        $server=$this->server;

        call_user_func(array($this,"login_$server"),$username,$password);
        $this->loginDone=true;

    }
    public function send($number,$msg)
    {
        $server=$this->server;
        if($this->loginDone)
        return call_user_func(array($this,"send_$server"),$number,$msg);
        else
        {
            echo "<h2>Please login first before sending SMS</h2>";
        }

    }
    private function login_way2sms($username,$password)
    {
        $out=($this->curl->post("http://www.way2sms.com","1=1"));
        $pattern="/Location:(.+?)\n/";
        preg_match($pattern,$out,$matches);
        $domain=trim($matches[1]);

        $this->data['domain']=$domain;

        $out= $this->curl->post("{$domain}auth.cl","username=$username&password=$password&Submit=Sign+in");

        $pattern="/Location:(.+?)\n/";
        preg_match($pattern,$out,$matches);
        $referer=trim($matches[1]);
        $this->data['referer']=$referer;

    }

    private function send_way2sms($number,$msg)
    {
        $domain=$this->data['domain'];
        $html=$this->curl->post("{$domain}jsp/InstantSMS.jsp?val=0","1=1",$this->data['referer']);
        if($this->debugMode)
        {
        echo "<h2>After logging in, the HTML returned by server is</h2>";
        echo $html;
        }

        $pattern = '/name="Action".+?value="(.*)"/';
        preg_match($pattern, $html, $matches);

        $custfrom=$matches[1];
        $msg=urlencode($msg);
        $html=$this->curl->post("{$domain}FirstServletsms?custid=","custid=undefined&HiddenAction=instantsms&Action={$custfrom}&login=&pass=&MobNo=$number&textArea=$msg");
        $pattern = '/class="style1">(.+?)<\/span>/';
        preg_match($pattern, $html, $matches);
        $out=($matches[1]);

        if(!preg_match("/successfully/",$out))
        {
        $this->setError($out);
        return false;
        }
        else
        {
        return true;
        $this->setError("No errors");
        }

    }
    public function getLastError()
    {
        return $this->error;

    }
    private function setError($error)
    {
        $this->error=$error;
    }
    private function login_160by2($username,$password)
    {
    //  $out2=$this->curl->get("http://m.160by2.com");
        $out=$this->curl->post("http://m.160by2.com/LoginCheck.asp?l=1&txt_msg=&mno=","txtUserName=$username&txtPasswd=$password&RememberMe=Yes&cmdSubmit=Login");
        $pattern="/MyMenu.asp\?Msg=(.+?)&/";

        preg_match($pattern,$out,$matches);
        $id=trim($matches[1]);
        $this->data['id']=$id;

    }

    private function send_160by2($number,$msg)
    {
        $msg=urlencode($msg);
        $id=$this->data['id'];
        $out1=$this->curl->post("http://m.160by2.com/SaveCompose.asp?l=1","txt_mobileno=$number&txt_msg=$msg&cmdSend=Send+SMS&TID=&T_MsgId=&Msg=$id");
        //echo $out1;
        $pattern = '/\<table.+?\>(.+)\<\/table/s';
        preg_match($pattern, $out1, $matches);

        $out=strip_tags(@$matches[1]);
        if(count($matches)<1)
        {
        $pattern="/\<div.+?background:.+?yellow.+?\>(.+?)\<\/div\>/s";

        preg_match($pattern,$out1,$matches);

        $out=strip_tags($matches[1]);
        }

//
    //  echo "out is $out";

        if(!preg_match("/successfully/i",$out))
        {

        $this->setError($out);

        return false;
        }
        else
        {
        return true;
        $this->setError("No errors");
        }

    }

    private function login_airtel($username,$password)
    {
        $this->data['username']=$username;
        $this->data['password']=$password;

    }

    private function send_airtel($number,$msg)
    {

    }
}

?>

// sms.php(错误日志)     在登录 ...     PHP错误消息

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/public_html/class.curl.php on line 70

Free Web Hosting

PHP Error Message

Notice: Undefined offset: 1 in /home/public_html/class.sms.php on line 126

Free Web Hosting
Sending SMS ... 
PHP Error Message

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/public_html/class.curl.php on line 70

Free Web Hosting
Error encountered :

1 个答案:

答案 0 :(得分:0)

您有两种情况......必须在您的php.ini上启用safe mode,或者必须设置open_basedir

解决问题......

<强>予。禁用安全模式。

打开php.ini并搜索此行

safe_mode = On

将其更改为

safe_mode = Off

<强> II。 Open_base目录

找到此行并在其前添加分号。

;open_basedir =

执行这些更改后,请保存文件并重新启动网络服务器。