短信与 Magento API 集成 [英] SMS Integration with Magento API

查看:25
本文介绍了短信与 Magento API 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Magento 1.8.1,我想将 SMS 与我们的商店集成.

I am using Magento 1.8.1 and I want to integrate SMS with our store.

我有 SMS 的 API URL,但不知道如何以及在 Magento 中放置该 URL.

I have an API URL of SMS but don't know how and where to put that URL in Magento.

他们给我提供了这个代码:

They provide me this code:

<?php 
class sendsms
{
 	private $api_url;
 	private $time;
 	private $unicode;
	private $working_key;
	private $start;
	private $sender_id;
	public  $api;
	public  $wk;
	public  $sid;
	public  $to;

	/**function to set the working key
	 * 
	 * @param string_type $wk:helps to change the working_key
	 */
	function setWorkingKey($wk)
	{   
		$this->working_key=$wk;
	}
	
	/**function to set sender id
	 * 
	 * @param string_type $sid:helps to change sender_id
	 */
	function setSenderId($sid)
	{   
		$this->sender_id=$sid;
	}

	/**function to set API url
	 * 
	 * @param string_type $apiurl:it is used to set api url
	 */
	function setapiurl($apiurl)
	{		$this->api=$apiurl;
			$a=strtolower(substr($apiurl,0,7));
			 
			 if ($a=="http://") //checking if already contains http://
			 {
			 	$api_url=substr($apiurl,7,strlen($apiurl));
			 	$this->api_url=$api_url;
			 	$this->start="http://";
			 }
		    elseif ($a=="https:/") //checking if already contains htps://
			 {
			 	$api_url=substr($apiurl,8,strlen($apiurl));
			 	$this->api_url=$api_url;
			 	$this->start="https://";
			 }
			 else { 
			 			$this->api_url=$apiurl;
			       		$this->start="http://";
			 	  }
	}

	/** function to intialize constructor
	 * 
	 * @param string_type $wk: it is working_key
	 * @param string_type $sd: it is sender_id
	 * @param string_type $apiurl: it is api_url
	 *          used for intializing the parameter
	 */
	function __construct($apiurl,$wk,$sd)
	{
		$this->setWorkingKey($wk);
		$this->setSenderId($sd);
		$this->setapiurl($apiurl);
	}

	/**
	 * function to send sms
	 * 
	 */
	function send_sms($to,$message,$dlr_url,$type="xml")
	{
		$this->process_sms($to,$message,$dlr_url,$type="xml",$time="null",$unicode="null");
	}

	/**
	 * function to schedule sms
	 * 
	 */
	function schedule_sms($to,$message,$dlr_url,$type="xml",$time)
	{ 
		$this->process_sms($to,$message,$dlr_url,$type="xml",$time,$unicode='');
	}

	/**
	 * function to send unicode message
	 */
	function unicode_sms($to,$message,$dlr_url,$type="xml",$unicode)
	{  
		$this->process_sms($to,$message,$dlr_url,$type="xml",$time='',$unicode);
	}

	/**
	 * function to send out sms
	 * @param string_type $to : is mobile number where message needs to be send
	 * @param string_type $message :it is message content
	 * @param string_type $dlr_url: it is used for delivering report to client
	 * @param string_type $type: type in which report is delivered
	 * @return output		$this->api=$apiurl;
	 */
	function process_sms($to,$message,$dlr_url="",$type="xml",$time='',$unicode='')
	{  
		$message=urlencode($message);
		$this->to=$to;
		$to=substr($to,-10) ;
		$arrayto=array("9", "8" ,"7");
		$to_check=substr($to,0,1);
	
	 if(in_array($to_check, $arrayto))
	 	$this->to=$to;
	 else echo "invalid number";

	if($time=='null')
		$time='';
	else
		$time="&time=$time";
	if($unicode=='null')
		$unicode='';
	else
		$unicode="&unicode=$unicode";
	
	 	
	 	$url="$this->start$this->api_url/web2sms.php?workingkey=$this->working_key&sender=$this->sender_id&to=$to&message=$message&type=$type&dlr_url=$dlr_url$time$unicode";
	 	$this->execute($url);
	}

	/**
	 * function to check message delivery status
	 * string_type $mid : it is message id 
	 */
	function messagedelivery_status($mid)
	{
		$url="$this->start$this->api_url/status.php?workingkey=$this->working_key&messageid=$mid";
			$this->execute($url);
	}

	/**
	 * function to check group message delivery
	 *  string_type $gid: it is group id
	 */
	function groupdelivery_status($gid)
	{
		 $url="$this->start$this->api_url/groupstatus.php?workingkey=$this->working_key&messagegid=$gid";
		$this->execute($url);
		
	}

	/**
	 * function to request to clent url
	 */
	function execute($url)
	{
		$ch=curl_init();
		// curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$output=curl_exec($ch);
		curl_close($ch);
		echo $output;
		return $output;
		
	}    
}

我是 Magento 的新手,所以请帮助我进行 API 集成.

I am new to Magento, so please help me with the API integration.

推荐答案

对于 SMS 集成,您需要决定要处理的事件.

For SMS Integration you need to decide what event you want to handle.

Magento 事件列表可在此处获得.

Magento Events List are available here.

之后,您需要为所选事件创建观察者.

After it you need to create observer for choosen event.

观察者是一个事件处理程序.它侦听它所附加的任何事件,并相应地对事件做出反应.

An Observer is an event handler. It listens to any event it is attached to and accordingly reacts to the event.

您的 SMS API 应该在观察者中使用.(它是PHP类中的方法.)

Your SMS API should be usen in observer. ( It is method in PHP class. )

要在 Magento 中创建观察者,您需要阅读本文档.

For creating observer in Magento you need to read this documentation.

这篇关于短信与 Magento API 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆