为vtiger创建自定义短信提供程序

时间:2014-03-25 16:21:06

标签: php linux vtiger

我试图为我的短信服务提供商修改vtiger crm附带的MyProvider.php。 我的短信服务提供商网址如下 http://sms.valueleaf.com/sms/user/urlsms.php?username=abc&pass=xyz&senderid=12345&message=hi你好吗&dest_mobileno = 91988000000& response = Y

但我写的程序不起作用。因为我不是一个php开发者,所以我很挣扎。任何人都可以看看并帮助我。

我编辑如下,但我认为还有一些语法错误。

#vim ValueLeaf.php
include_once dirname(__FILE__) . '/../ISMSProvider.php';
include_once 'vtlib/Vtiger/Net/Client.php';

class ValueLeaf implements ISMSProvider {

private $_username;
private $_password;
private $_parameters = array();

const SERVICE_URI = 'sms.valueleaf.com';

**//I added senderid and response to array to include in the url.**
private static $REQUIRED_PARAMETERS = array('senderid','response');

function __construct() {
}

public function setAuthParameters($username, $password) {
$this->_username = $username;
$this->_password = $password;
}

public function setParameter($key, $value) {
$this->_parameters[$key] = $value;
}

public function getParameter($key, $defvalue = false) {
if(isset($this->_parameters[$key])) {
return $this->_parameters[$key];
}
return $defvalue;
}
public function getRequiredParams() {
return self::$REQUIRED_PARAMETERS;
}

public function getServiceURL($type = false) {
if($type) {
switch(strtoupper($type)) {

**//As I dont use authentication i commented it.**
case self::SERVICE_AUTH: return self::SERVICE_URI; // . '/http/auth';
case self::SERVICE_SEND: return self::SERVICE_URI . '/sms/user/urllongsms.php';
case self::SERVICE_QUERY: return self::SERVICE_URI . '/sms/user/responce.php';

}
}
return false;
}

protected function prepareParameters() {
**//extended to get and set the additional parameters**
$params = array('username' => $this->_username, 'pass' => $this->_password, 'senderid' => $this->senderid,'response'=> $this->response);
//$params = array();
foreach (self::$REQUIRED_PARAMETERS as $key) {
$params[$key] = $this->getParameter($key);
}
return $params;
}

**//Here I'm little confused.Actually in the actual file its $tonumbers. But in the http api its like dest_mobileno=91988000000. I dont know where to change it.**
public function send($message, $tonumbers) {
if(!is_array($tonumbers)) {
$tonumbers = array($tonumbers);
}

$params = $this->prepareParameters();
$params['text'] = $message;
$params['to'] = implode(',', $tonumbers);

$serviceURL = $this->getServiceURL(self::SERVICE_SEND);
$httpClient = new Vtiger_Net_Client($serviceURL);
$response = $httpClient->doPost($params);

$responseLines = split("\n", $response);

$results = array();
foreach($responseLines as $responseLine) {

$responseLine = trim($responseLine);
if(empty($responseLine)) continue;

$result = array( 'error' => false, 'statusmessage' => '' );
if(preg_match("/ERR:(.*)/", trim($responseLine), $matches)) {
$result['error'] = true;
$result['to'] = $tonumbers[$i++];
$result['statusmessage'] = $matches[0]; // Complete error message
} else if(preg_match("/ID: ([^ ]+)TO:(.*)/", $responseLine, $matches)) {
$result['id'] = trim($matches[1]);
$result['to'] = trim($matches[2]);
$result['status'] = self::MSG_STATUS_PROCESSING;

} else if(preg_match("/ID: (.*)/", $responseLine, $matches)) {
$result['id'] = trim($matches[1]);
$result['to'] = $tonumbers[0];
$result['status'] = self::MSG_STATUS_PROCESSING;
}
$results[] = $result;
}
return $results;
}

**//I'm not checking the query fucntion now. First thing is to send and recieve.**
public function query($messageid) {

//$params = $this->prepareParameters();
$params['workingkey'] = $REQUIRED_PARAMETERS('workingkey');
$params['messageid'] = $messageid;

$serviceURL = $this->getServiceURL(self::SERVICE_QUERY);
$httpClient = new Vtiger_Net_Client($serviceURL);
$response = $httpClient->doPost($params);

$response = trim($response);

$result = array( 'error' => false, 'needlookup' => 1 );

if(preg_match("/ERR: (.*)/", $response, $matches)) {
$result['error'] = true;
$result['needlookup'] = 0;
$result['statusmessage'] = $matches[0];

} else if(preg_match("/ID: ([^ ]+) Status: ([^ ]+)/", $response, $matches)) {
$result['id'] = trim($matches[1]);
$status = trim($matches[2]);

// Capture the status code as message by default.
$result['statusmessage'] = "CODE: $status";

if($status === '1') {
$result['status'] = self::MSG_STATUS_PROCESSING;
} else if($status === '2') {
$result['status'] = self::MSG_STATUS_DISPATCHED;
$result['needlookup'] = 0;
}
}

return $result;
}
}
?>

我正在编辑代码并检查。我仍然无法得到任何结果。 我已经给printf获取最终的url,这是去sms提供者的。但没有得到。有什么想法,我必须给出正确的printf来获得最终细节吗?

更新的代码:  1. sms.php

<?php
/*+**********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 ************************************************************************************/
include_once dirname(__FILE__) . '/../ISMSProvider.php';
include_once 'vtlib/Vtiger/Net/Client.php';

class sms implements ISMSProvider {

    private $_username;
    private $_password;
    private $_parameters = array();

    const SERVICE_URI = 'http://sms.valueleaf.com';
    private static $REQUIRED_PARAMETERS = array('senderid','response');
    function __construct() {        
    }

    public function setAuthParameters($username, $password) {
        $this->_username = $username;
        $this->_password = $password;
    }

    public function setParameter($key, $value) {
        $this->_parameters[$key] = $value;
    }

    public function getParameter($key, $defvalue = false)  {
        if(isset($this->_parameters[$key])) {
            return $this->_parameters[$key];
        }
        return $defvalue;
    }

    public function getRequiredParams() {
        return self::$REQUIRED_PARAMETERS;
    }

    public function getServiceURL($type = false) {      
        if($type) {
            switch(strtoupper($type)) {

                case self::SERVICE_AUTH: return  self::SERVICE_URI; // . '/http/auth';
                case self::SERVICE_SEND: return  self::SERVICE_URI . '/sms/user/urllongsms.php?';
                case self::SERVICE_QUERY: return self::SERVICE_URI . '/sms/user/responce.php?';

            }
        }
        return false;
    }

    protected function prepareParameters() {
        $params = array('username' => $this->_username, 'pass' => $this->_password);
        //$params = array();
        foreach (self::$REQUIRED_PARAMETERS as $key) {
            $params[$key] = $this->getParameter($key);
        }
        return $params;
    }

        //$file0 = fopen("test0.txt","w");
                //echo fprintf($file0,"came before send");
    public function send($message, $tonumbers) {
        if(!is_array($tonumbers)) {
            $tonumbers = array($tonumbers);
        }

        $params = $this->prepareParameters();
        //$params['text'] = $message;
        $params['message'] = $message;
        //$params['to'] = implode(',', $tonumbers);
        $params['dest_mobileno'] = implode(',', $tonumbers);

        $serviceURL = $this->getServiceURL(self::SERVICE_SEND);     
        $file = fopen("test.txt","w");
        echo fprintf($file,"url is %s.",$serviceURL);
        $httpClient = new Vtiger_Net_Client($serviceURL);
        $file1 = fopen("test1.txt","w");
        $response = $httpClient->doPost($params);       

        foreach (self::$REQUIRED_PARAMETERS as $key) {
            echo fprintf($file1,"String %s value is %s.",$key,$params[$key]);
        }
        echo fprintf($file1,"Message is: %s",$message);
        echo fprintf($file1,"dest_mobileno is: %s",$tonumbers[0]);
        echo fprintf($file1,"Response is %s.",$response);
        $responseLines = split("\n", $response);        

        $results = array();
        foreach($responseLines as $responseLine) {

            $responseLine = trim($responseLine);            
            if(empty($responseLine)) continue;

            $result = array( 'error' => false, 'statusmessage' => '' );
            if(preg_match("/ERR:(.*)/", trim($responseLine), $matches)) {
                $result['error'] = true; 
                $result['to'] = $tonumbers[$i++];
                $result['statusmessage'] = $matches[0]; // Complete error message
            } else if(preg_match("/ID: ([^ ]+)TO:(.*)/", $responseLine, $matches)) {
                $result['id'] = trim($matches[1]);
                $result['to'] = trim($matches[2]);
                $result['status'] = self::MSG_STATUS_PROCESSING;

            } else if(preg_match("/ID: (.*)/", $responseLine, $matches)) {
                $result['id'] = trim($matches[1]);
                $result['to'] = $tonumbers[0];
                $result['status'] = self::MSG_STATUS_PROCESSING;
            }
            $results[] = $result;
        }       
        return $results;
    }


    public function query($messageid) {

        //$params = $this->prepareParameters();
//      $params['workingkey'] = $REQUIRED_PARAMETERS('workingkey');
        $params['messageid'] = $messageid;

        $serviceURL = $this->getServiceURL(self::SERVICE_QUERY);
        $httpClient = new Vtiger_Net_Client($serviceURL);
        $response = $httpClient->doPost($params);

        $response = trim($response);

        $result = array( 'error' => false, 'needlookup' => 1 );

        if(preg_match("/ERR: (.*)/", $response, $matches)) {
            $result['error'] = true;
            $result['needlookup'] = 0;
            $result['statusmessage'] = $matches[0];

        } else if(preg_match("/ID: ([^ ]+) Status: ([^ ]+)/", $response, $matches)) {
            $result['id'] = trim($matches[1]);
            $status = trim($matches[2]);

            // Capture the status code as message by default.
            $result['statusmessage'] = "CODE: $status";

            if($status === '1') {
                $result['status'] = self::MSG_STATUS_PROCESSING;
            } else if($status === '2') {
                $result['status'] = self::MSG_STATUS_DISPATCHED;
                $result['needlookup'] = 0;
            }
        } 

        return $result;
    } 
}

?>
  1. ISMSProvider.php

    / + ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * **

    • 此文件的内容受vtiger CRM公共许可证版本1.0
    • 的约束
    • (&#34;许可&#34);除非符合许可,否则您不得使用此文件
    • 原始代码是:vtiger CRM开源
    • 原始代码的初始开发人员是vtiger。
    • vtiger创建的部分版权归(C)vtiger所有。
    • 保留所有权利。 的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** / 接口ISMSProvider {

      const MSG_STATUS_DISPATCHED =&#34;已发送&#34 ;; const MSG_STATUS_PROCESSING =&#34; Processing&#34 ;; const MSG_STATUS_DELIVERED =&#34; Delivered&#34 ;; const MSG_STATUS_FAILED =&#34;失败&#34 ;; const MSG_STATUS_ERROR =&#34; ERR:&#34 ;;

      const SERVICE_SEND =&#34; SEND&#34 ;; const SERVICE_QUERY =&#34; QUERY&#34 ;; const SERVICE_PING =&#34; PING&#34 ;; const SERVICE_AUTH =&#34; AUTH&#34 ;;

      / **

      • 获取(用户名,密码)以外的必需参数 * / 公共函数getRequiredParams();

      / **

      • 获取用于给定类型的服务URL *
      • @param String $类型,如SEND,PING,QUERY * / 公共函数getServiceURL($ type = false);

      / **

      • 设置验证参数 *
      • @param String $ username
      • @param String $ password * / public function setAuthParameters($ username,$ password);

      / **

      • 设置非auth参数。 *
      • @param String $ key
      • @param String $ value * / public function setParameter($ key,$ value);

      / **

      • 处理短信发送操作 *
      • @param String $ message
      • @param mixed $ tonumbers一个或一组数字 * / 公共功能发送($ message,$ tonumbers);

      / **

      • 使用messgae id查询状态 *
      • @param String $ messageid * / 公共函数查询($ messageid);

    }

  2. Client.php

    / + ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ***

    • 此文件的内容受vtiger CRM公共许可证版本1.0
    • 的约束
    • (&#34;许可&#34);除非符合许可,否则您不得使用此文件
    • 原始代码是:vtiger CRM开源
    • 原始代码的初始开发人员是vtiger。
    • vtiger创建的部分版权归(C)vtiger所有。
    • 保留所有权利。 的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** / 包括&#39; vtlib / thirdparty / network / Request.php&#39;;

    / **

    • 提供使用HTTP连接的API。
    • @package vtlib * / class Vtiger_Net_Client { var $ client; var $ url; var $ response;

      / **

      • 构造
      • @param字符串网站的网址
      • 示例:
      • $ client = new Vtiger_New_Client(&#39; http://www.vtiger.com&#39;); * / function __construct($ url){ $这 - &GT; setURL($网址); }

      / **

      • 为此实例设置另一个网址
      • @param String要使用的URL前进 * / function setURL($ url){ $ this-&gt; url = $ url; $ this-&gt; client = new HTTP_Request(); $ this-&gt; response = false; }

      / **

      • 设置自定义HTTP标头
      • @param映射HTTP标头和值对 * / function setHeaders($ values){ foreach($ values as $ key =&gt; $ value){     $ this-&gt; client-&gt; addHeader($ key,$ value); } }

      / **

      • 执行GET请求
      • @param映射键值对或false
      • @param整数超时值 * / function doGet($ params = false,$ timeout = null){ if($ timeout)$ this-&gt; client-&gt; _timeout = $ timeout; $这 - &GT;客户机&GT; setURL($这 - &GT; URL); $这 - &GT;客户机&GT;使用setMethod(HTTP_REQUEST_METHOD_GET);

        if($ params){     foreach($ params as $ key =&gt; $ value)         $ this-&gt; client-&gt; addQueryString($ key,$ value); } $ this-&gt; response = $ this-&gt; client-&gt; sendRequest();

        $ content = false; if(!$ this-&gt; wasError()){     $ content = $ this-&gt; client-&gt; getResponseBody(); } $这 - &GT;断开(); 返回$ content; }

      / **

      • 执行POST请求
      • @param映射键值对或false
      • @param整数超时值 * / function doPost($ params = false,$ timeout = null){ if($ timeout)$ this-&gt; client-&gt; _timeout = $ timeout; $这 - &GT;客户机&GT; setURL($这 - &GT; URL); $这 - &GT;客户机&GT;使用setMethod(HTTP_REQUEST_METHOD_POST);

        if($ params){     if(is_string($ params))$ this-&gt; client-&gt; addRawPostData($ params);     其他{         foreach($ params as $ key =&gt; $ value)             $ this-&gt; client-&gt; addPostData($ key,$ value);     } } $ this-&gt; response = $ this-&gt; client-&gt; sendRequest();

        $ content = false; if(!$ this-&gt; wasError()){     $ content = $ this-&gt; client-&gt; getResponseBody(); } $这 - &GT;断开();

        返回$ content; }

      / **

      • 上次请求导致错误吗? * / function isError(){ return PEAR :: isError($ this-&gt; response); }

      / **

      • 断开此实例 * / function disconnect(){ $这 - &GT;客户机&GT;断开(); } }
  3. 任何人都可以用这个来帮助我。短信根本不会发生。 测试printfs中的输出:

    [root@ providers]# cat /var/www/html/vtigercrm/test.txt
    url is http://sms.valueleaf.com/sms/user/urllongsms.php?.
    
    [root@ providers]# cat /var/www/html/vtigercrm/test1.txt
    String senderid value is 066645.String response value is Y.Message is: Vtiger test on 8.57dest_mobileno is: ArrayResponse is
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    
    <head>
      <title>sms.valueleaf.com</title>
    
    </head>
    <frameset rows="100%,*" border="0">
      <frame src="http://203.129.203.254/sms/user/urllongsms.php" frameborder="0" />
      <frame frameborder="0" noresize />
    </frameset>
    
    <!-- pageok -->
    <!-- 04 -->
    <!-- -->
    </html>.
    

    任何形式的帮助将不胜感激。 :(

1 个答案:

答案 0 :(得分:1)

您的代码中没有语法错误。这是一个工作脚本,您可以根据您的短信提供商进行修改并进行配置。

    private $username;
    private $password;
    private $parameters = array();

    const SERVICE_URI = 'https://trans.springedge.com/api/';

    private static $REQUIRED_PARAMETERS = array(
        array('name' => 'apikey', 'label' => 'API Key', 'type' => 'text'),
        array('name' => 'sender', 'label' => 'Sender ID', 'type' => 'text'),
        array('name' => 'unicode', 'label' => 'Character Set', 'type' => 'picklist', 'picklistvalues' => array('1' => 'Unicode', '0' => 'GSM', 'auto' => 'Auto Detect'))
    );

    public function getName() {
        return 'SpringEdge';
    }

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

    public function setParameter($key, $value) {
        $this->parameters[$key] = $value;
    }

    public function getParameter($key, $defaultvalue = false) {
        if (isset($this->parameters[$key])) {
            return $this->parameters[$key];
        }
        return $defaultvalue;
    }

    public function getRequiredParams() {
        return self::$REQUIRED_PARAMETERS;
    }

    public function getServiceURL($type = false) {
        if ($type) {
            switch (strtoupper($type)) {
                case self::SERVICE_SEND : return self::SERVICE_URI . '/web/send/';
                case self::SERVICE_QUERY : return self::SERVICE_URI . '/status/message/';
            }
        }
        return false;
    }

    protected function prepareParameters() {
        foreach (self::$REQUIRED_PARAMETERS as $requiredParam) {
            $paramName = $requiredParam['name'];
            $params[$paramName] = $this->getParameter($paramName);
        }
        $params['format'] = 'json';
        return $params;
    }

    public function send($message, $tonumbers) {
        if (!is_array($tonumbers)) {
            $tonumbers = array($tonumbers);
        }
        foreach ($tonumbers as $i => $tonumber) {
            $tonumbers[$i] = str_replace(array('(', ')', ' ', '-'), '', $tonumber);
        }

        $params = $this->prepareParameters();
        $params['message'] = $message;
        $params['to'] = implode(',', $tonumbers);

        $serviceURL = $this->getServiceURL(self::SERVICE_SEND);
        $httpClient = new Vtiger_Net_Client($serviceURL);
        $response = $httpClient->doGet($params);
        $rows = json_decode($response, true);

        $numbers = explode(',', $params['to']);
        $results = array();
        $i = 0;

        if ($rows['status'] != 'OK') {
            foreach ($numbers as $number) {
                $result = array();
                $result['to'] = $number;
                $result['error'] = true;
                $result['statusmessage'] = $rows['message'];
                $result['id'] = $rows['data'][$i++]['id'];
                $result['status'] = self::MSG_STATUS_ERROR;
                $results[] = $result;
            }
        }else{
            foreach ($rows['data'] as $value) {
                if (is_array($value)) {
                    $result = array();
                    $result['error'] = false;
                    $result['to'] = $value['mobile'];
                    $result['id'] = $value['id'];
                    $result['statusmessage'] = $rows['message'];
                    $result['status'] = $this->checkstatus($value['status']);
                    $results[] = $result;
                }
            }
        }
        return $results;
    }

    public function checkstatus($status) {
        if ($status == 'AWAITED-DLR') {
            $result = self::MSG_STATUS_PROCESSING;
        } elseif ($status == 'DELIVRD') {
            $result = self::MSG_STATUS_DELIVERED;
        } else {
            $result = self::MSG_STATUS_FAILED;
        }
        return $result;
    }

    public function query($messageid) {
        $params = $this->prepareParameters();
        $params['messageid'] = $messageid;
        $serviceURL = $this->getServiceURL(self::SERVICE_QUERY);
        $httpClient = new Vtiger_Net_Client($serviceURL);
        $response = $httpClient->doGet($params);
        $rows = json_decode($response, true);
        $result = array();
        if ($rows['status'] != 'OK') {
            $result['error'] = true;
            $result['status'] = self::MSG_STATUS_ERROR;
            $result['needlookup'] = 1;
            $result['statusmessage'] = $rows['message'];
        } else {
            $result['error'] = false;
            $result['status'] = $this->checkstatus($rows['data']['0']['status']);
            $result['needlookup'] = 0;
            $result['statusmessage'] = $rows['message'];
        }
        return $result;
    }

    function getProviderEditFieldTemplateName() {
        return 'BaseProviderEditFields.tpl';
    }
}

将上述代码保存为/ modules / SMSNotifier / providers中的sms_provider_name.php以供使用。