Jump to content

Recommended Posts

I'm afraid this is a bit of a long winded introduction for what I think would be a simple question:

 

For reference, I'm trying to modify the SMS API kindly provided at Sourceforge (http://sourceforge.net/projects/sms-api/) to something that will send WAP PUSH messages. I have tried my best to modify it as much as I can without changing the essentials, but for some reason, I keep receiving an fopen error built into the script that informs me the script has died.

 

The original (almost) working SMS API is something like this (with all the unnecssary bits cut out):

 

     function send($to=null, $from=null, $text=null, $msg_type=null, $climsgid=null) {

    	/* Check SMS credits balance */
    	if ($this->getbalance() < $this->balace_limit) {
    	    die ("You have reach the SMS credit limit!");
    	};

    	/* Check SMS $text length */
        if ($this->unicode == true) {
            $this->_chk_mbstring();
            if (mb_strlen ($text) > 210) {
        	    die ("Your unicode message is to long! (Current length".mb_strlen ($text).")");
        	}
        	/* Does message need to be concatenate */
            if (mb_strlen ($text) > 70) {
                $concat = "&concat=3";
        	} else {
                $concat = "";
            }
        } else {
            if (strlen ($text) > 465) {
    	        die ("Your message is to long! (Current length=".strlen ($text).")");
    	    }
        	/* Does message need to be concatenate */
            if (strlen ($text) > 160) {
                $concat = "&concat=3";
        	} else {
                $concat = "";
            }
        }

	/* collect message type from form */
	$msg_type = $_POST["msg_type"];		

	/*collect client message id */
	$climsgid = $_POST["climsgid"];

	/*collect priority value */
	$escalate = $_POST["escalate"];

    	/* Check $to and $from is not empty */
        if (empty ($to)) {
    	    die ("You have not specified a destination address (TO)!");
    	}
        if (empty ($from)) {
    	    die ("You have not specified a source address (FROM)!");
    	}
        if (empty ($msg_type)) {
    	    die ("You have not specified a message type (MESSAGE TYPE)!");
    	}		

    	/* Reformat $to number */
        $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
        $to = str_replace($cleanup_chr, "", $to);


    	/* Send SMS now */
    	$comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&msg_type=%s&climsgid=%s&escalate=%s&callback=%s&unicode=%s%s",
            $this->base,
            $this->session,
            rawurlencode($to),
            rawurlencode($from),
            $this->encode_message($text),
            $msg_type,
		$climsgid,
		$escalate,
            $this->callback,
            $this->unicode,
            $concat
        );
        return $this->_parse_send ($this->_execgw($comm));
    }

    /**
    * Encode message text according to required standard
    * @param text mixed  Input text of message.
    * @return mixed  Return encoded text of message.
    * @access public
    */
    function encode_message ($text) {
        if ($this->unicode != true) {
            //standard encoding
            return rawurlencode($text);
        } else {
            //unicode encoding
            $uni_text_len = mb_strlen ($text, "UTF-8");
            $out_text = "";

            //encode each character in text
            for ($i=0; $i<$uni_text_len; $i++) {
                $out_text .= $this->uniord(mb_substr ($text, $i, 1, "UTF-8"));
            }

            return $out_text;
        }
    }

/**
    * Execute gateway commands
    * @access private
    */
    function _execgw($command) {
        if ($this->sending_method == "curl")
            return $this->_curl($command);
        if ($this->sending_method == "fopen")
            return $this->_fopen($command);
        die ("Unsupported sending method!");
    }

    /**
    * fopen sending method
    * @access private
    */
    function _fopen($command) {
        $result = '';
        $handler = @fopen ($command, 'r');
        if ($handler) {
            while ($line = @fgets($handler,1024)) {
                $result .= $line;
            }
            fclose ($handler);
            return $result;
        } else {
            die ("Error while executing fopen sending method!<br>Please check does PHP have OpenSSL support and check does PHP version is greater than 4.3.0.");
        }
    }

    /**
    * Parse authentication command response text
    * @access private
    */
    function _parse_auth ($result) {
    	$session = substr($result, 4);
        $code = substr($result, 0, 2);
        if ($code!="OK") {
            die ("Error in SMS authorization! ($result)");
        }
        return $session;
    }

    /**
    * Parse send command response text
    * @access private
    */
    function _parse_send ($result) {
    	$code = substr($result, 0, 2);
    	if ($code!="ID") {
    	    die ("Error sending SMS! ($result)");
    	} else {
    	    $code = "OK - The message id is #$apiMsgId";
    	}
        return $code;
    }


 

 

The only parts I've changed are:

    function send($to=null, $from=null, $climsgid=null, $si_url=null, $si_text=null) {

    	/* Check SMS credits balance */
    	if ($this->getbalance() < $this->balace_limit) {
    	    die ("You have reach the credit limit!");
    	};

       	/* Check $to and $from is not empty */
        if (empty ($to)) {
    	    die ("You did not specify destination address (TO)!");
    	}
        if (empty ($from)) {
    	    die ("You did not specify source address (FROM)!");
    	}
        if (empty ($si_url)) {
    	    die ("You did not specify the URL (URL)!");
    	}
        if (empty ($si_text)) {
    	    die ("You did not specify the message (Message)!");
    	}		

    	/* Reformat $to number */
        $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
        $to = str_replace($cleanup_chr, "", $to);

	/* Pickup extra values */
	$climsgid = $_POST["climsgid"];
	$si_url = $_POST["si_url"];
	$si_text = $_POST["si_text"];
	$si_action = $_POST["si_action"];

    	/* Send Wap Push now */
    	$comm = sprintf ("%s/si_push?session_id=%s&to=%s&from=%s&climsgid=%s&si_id&si_url=%s&si_text=%s&si_created&si_expires&si_action=%s%s",
		$this->base,
            $this->session,
		$this->api_id, 
		$this->user, 
		$this->password,
            rawurlencode($to),
            rawurlencode($from),
		$climsgid,
		rawurlencode($si_url),
            $this->encode_message($si_text),
            $si_action,
            $concat
        );
        return $this->_parse_send ($this->_execgw($comm));
    }

    /**
    * Encode message text according to required standard
    * @param text mixed  Input text of message.
    * @return mixed  Return encoded text of message.
    * @access public
    */
    function encode_message ($si_text) {
        if ($this->unicode != true) {
            //standard encoding
            return rawurlencode($si_text);
        } else {
            //unicode encoding
            $uni_text_len = mb_strlen ($si_text, "UTF-8");
            $out_text = "";

            //encode each character in text
            for ($i=0; $i<$uni_text_len; $i++) {
                $out_text .= $this->uniord(mb_substr ($si_text, $i, 1, "UTF-8"));
            }

            return $out_text;
        }
    }

 

 

And the error I receive is : to check that PHP > 4.3 and server supports OpenSSL, which I know it does, because the first SMS part works.

 

I have a feeling it's the way I've concatenated the string, which is causing the fopen error. So would really appreciate it if anyone had any suggestions as I'm really starting to pull my hair out >_<

 

Thanks in advance :)

 

Link to comment
https://forums.phpfreaks.com/topic/66841-sms-api-fopen-question-really/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.