back to the text/html should be problem

3 views
Skip to first unread message

TedinHawaii

unread,
Mar 6, 2011, 10:31:59 PM3/6/11
to Zeep Mobile
OK, I am confused. First, I am writing in PHP. I am not even handling
signups right now, I am trying to make this work with a single number
I have signed up. So far, I can send a message from the phone, it's
received properly and the proper message is sent back and then a
second message is sent to the mobile complaining about the text/html
problem. I have read a lot of comments in the group about this and
read all the sample code. It's left me confused. I have a function
that sends the response to Zeep: See below

<?function sendsms($number , $message)
{
define( API_URL, 'https://api.zeepmobile.com/messaging/2008-07-14/
send_message' );
define( API_KEY, 'mykeyhere' );
define( SECRET_ACCESS_KEY, 'myaccesskeyhere' );
$http_date = gmdate( DATE_RFC822 );
$parameters = "user_id=" . rawurlencode($number) .
"&body=".rawurlencode($message); //version with urlencoded id per
http://aboutdev.wordpress.com/2009/02/09/zeep-mobile-sms-with-ads/
$canonical_string = API_KEY . $http_date . $parameters;
$b64_mac = base64_encode(hash_hmac("sha1",
$canonical_string,SECRET_ACCESS_KEY, TRUE));
$authentication = "Zeep " . API_KEY . ":$b64_mac";

$header = array(
"Authorization: ".$authentication,
"Date: ".$http_date,
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: " . strval(strlen($parameters))
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, API_URL );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters );
$response = curl_exec($ch);
curl_close($ch);
return $response;
} ?>

Now in this function there is a header and this is what is required to
send the response to Veep. I tried changing it and things fail. So,
this works. Now, here is the code that calls this function. This is in
an index.php file and is the callback URL so this is what gets called
when people send an SMS.

<?php
include("zeepfunctions.php");

$event = trim($_POST['event']);
switch ($event) {

case 'SUBSCRIPTION_UPDATE':
$userId = $_POST['uid'];
$phoneNumber = $_POST['min'];
break;

case 'MO':
$userId = trim($_POST['uid']);

$msg = $_POST['body'];
$query = "select * from reviewed_thing where thing_description
LIKE '$msg' ";
//dbprocessing here that creates a real and short text message
instead of My Message below
$message = My message";
$result = sendsms($userId , $message);
break;
}

?>

You can see that I'm doing nothing about the subscribe at the moment.
Now, I've read where you have to set a header to content-type: text/
plain using the PHP header() function, but I have no idea where this
should go. Do I build my text message using the headers and add these
headers before my actual text message that gets sent to veep using the
function above? Help!!!

I am so close. This is all working well except that I get a second
text message with the error.

Ted

TedinHawaii

unread,
Mar 6, 2011, 11:00:41 PM3/6/11
to Zeep Mobile
I am replying to my own message because I have more information. I
have code that works and code that does not work and I don't know what
the difference is. Both small code snippets call a function that sends
the message. First, here is the function:

<?function sendsms($number , $message)
{
header("Status: 200 OK");
header("Date: ".gmdate(DATE_RFC822));
header("Content-Type: text/plain");
header("Content-Length: " . strval(strlen($message)));

define( API_URL, 'https://api.zeepmobile.com/messaging/2008-07-14/
send_message' );
define( API_KEY, 'mykey' );
define( SECRET_ACCESS_KEY, 'xxxxxxxxxxxxxxxxxx' );
You can see that grasping at straws I added the header functions at
the top compared to what I sent before. NOW, here is the weird part,
the first code snippet works and sends the message without the
accompanying second complaint message and the second one sends the
message but gets the complaint:

<?
include('zeepfunctions.php');
$number = "+18087690154";
$message = "these are the times that try mens souls";
$result = sendsms($number , $message);
echo $result;
?>
Works good.

<?php
include("zeepfunctions.php");
$event = trim($_POST['event']);
switch ($event) {
case 'SUBSCRIPTION_UPDATE':
$userId = $_POST['uid'];
$phoneNumber = $_POST['min'];
break;
case 'MO':
$userId = trim($_POST['uid']);
$msg = $_POST['body'];
$message=get_review($msg);
$result = sendsms($userId , $message);
break;
}
?>
works but gets complaint message. thanks for your patience. For people
looking for PHP sample code this all sorta works.

Ted

Dustin Oprea

unread,
Mar 7, 2011, 12:13:34 AM3/7/11
to zeep-...@googlegroups.com, TedinHawaii
It seems like you're really having a problem over there. In the interest of making this quick, here's my send code:

class SMS_Senders_Zeep extends StandardBaseClass implements MessageSender
{

    private $logger, $url, $apiKey, $secretKey;

    function __construct()
    {

        $this->logger = $this->getLogger('SMSSenderZeep');
    }

    function setParameters($parameters)
    {

        $missing = -1;
        if(StandardMisc::VerifyArrayKeys($parameters, array('url', 'apiKey', 'secretKey'), $matched) == false)
        {
            $this->logger->error("There were one or more missing parameters: " . implode(', ', $missing));
            return false;
        }
   
        $this->url          = $parameters['url'];
        $this->apiKey       = $parameters['apiKey'];
        $this->secretKey    = $parameters['secretKey'];

        return true;
    }

    function send($ani, $message)
    {

        $message = trim($message);

        if(empty($message))
        {
            $this->logger->error("Message to [$ani] is empty. We're refusing to send.");
            return false;
        }

        $query = http_build_query(array(
                                                'user_id'   => "+$ani",
                                                'body'      => $message,
                                            ));

        $httpDate = gmdate(DATE_RFC822);
        $canonical_string = "{$this->apiKey}{$httpDate}{$query}";
 
        $b64MAC = base64_encode(hash_hmac("sha1", $canonical_string, $this->secretKey, true));
 
        $authentication = "Zeep {$this->apiKey}:$b64MAC";
 
        $header = array(
                        "Authorization: $authentication",
                        "Date: $httpDate",
                        "Content-Type: application/x-www-form-urlencoded",
                        "Content-Length: " . strval(strlen($query)),
                    );

        $this->logger->info("Pushing message of (" . strlen($query) . ") bytes to [$ani].");

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,               $this->url);

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,    true);
        curl_setopt($ch, CURLOPT_HTTPHEADER,        $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,    true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,    false);
        curl_setopt($ch, CURLOPT_POST,              true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,        $query);

        // Force a re-used connection.
        curl_setopt($ch, CURLOPT_FORBID_REUSE,      0);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT,     0);

        if(($response = curl_exec($ch)) === false)
        {
            $this->logger->error("Could not send SMS request: " . curl_error($ch));
            return false;
        }

        $this->logger->info("Message sent to [$ani].");

        return true;
    }
}


Cheers.

Dustin



Ted

--
You received this message because you are subscribed to the Google Groups "Zeep Mobile" group.
To post to this group, send email to zeep-...@googlegroups.com.
To unsubscribe from this group, send email to zeep-mobile...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/zeep-mobile?hl=en.


Reply all
Reply to author
Forward
0 new messages