Php sms virtual clinic

I am presently having difficulty in fixing my php code. I am developing an SMS virtual clinic, that will allow the user to inquire via SMS regarding their illness’s possible cause and it’s medication. Here is my php code, receiving SMS from the user.

PHP:

<?php
$json = file_get_contents('php://input');
    $json = stripslashes($json);
    $values = json_decode($json, true);

Array (
   "inboundSMSMessageList" => Array (
       "inboundSMSMessage" => Array (
          [0] => Array (
             "dateTime" => "Wed February 25 2014 12:12:13 GMT+0000 (UTC)",
             "destinationAddress" => "21587258",
             "messageId" => null,
             "message" => "Hello",
             "resourceURL" => null,
             "senderAddress" => "9152886810"
          )
        ),
        "numberOfMessagesInThisBatch" => 1,
        "resourceURL" => null,
        "totalNumberOfPendingMessages" => null
    )
)
?>

The question is: Where i put the code for registration, inquiry and receiving notification from the system.

I am presently developing an SMS application entitled " SMS Virtual Clinic, which aims to help patient to know the possible cause or diagnose their illness as they send SMS keyword like “medication” and send to my short code number. Here’s what i did:

<?php

$globe = new GlobeApi('v1');
$sms = $globe->sms([87258]);
$response = $sms->sendMessage([access_token], [number], [message]);

Array (
  "outboundSMSMessageRequest" => Array (
    "address" => "0915xxxxxxx",
    "deliveryInfoList" => Array (
      "deliveryInfo" => [],
      "resourceURL" => null
    ),
    "senderAddress" => "21587",
    "outboundSMSTextMessage" => Array (
      "message": " Diagnose with  "
    ),
    "receiptRequest" => Array (
      "notifyURL" => null,
      "callbackData" => null,
      "senderName" => null,
      "resourceURL" => null
    )
  )
)
?>

This code will send message from 21587
And here’s my receive SMS php code:

<?php
$json = file_get_contents('php://input');
    $json = stripslashes($json);
    $values = json_decode($json, true);

Array {
   "inboundSMSMessageList" => Array {
       "inboundSMSMessage" => Array {
          [0] => Array {
             "dateTime" => "Tue March 11 2014 12:12:13 GMT

+0000 (UTC)",
             "destinationAddress" => "2xxxxxx",
             "messageId" => null,
             "message" => "Hello",
             "resourceURL" => null,
             "senderAddress" => "915xxxxxxx"
          }
        },
        "numberOfMessagesInThisBatch" => 1,
        "resourceURL" => null,
        "totalNumberOfPendingMessages" => null
    }
}
$list[$name] = mysql_escape_string($value);
 list($action, $name, $address, $course, $yearlevel)=explode

('/',$list);
   if($action == 'reg')
{
$db=mysql_connect("localhost", "root", "******") or die

("Cannot connect to DB!");
mysql_select_db("cyfdb") or die("Cannot select DB!");
$sql="INSERT INTO mydb (name,address,course,yearlevel)
VALUES ('$name','$address','$course','$yearlevel')";
$r = mysql_query($sql);
if(!$r) {

    $err=mysql_error();

    print $err;

    exit();}
$mysql_close($db);


}	
else if($action=='inq')

{
$link=mysql_connect("localhost", "root", "******") or die

("Cannot connect to DB!");
mysql_select_db("mydb") or die("Cannot select DB!");
$sql = "SELECT event,location,time from activity";
while($r = mysql_fetch_array($sql)){
	  $items[] = array('event'=>$r['event'],
                          'location'=>$r['location'],
                          'date'=>$r['date']);
	}
	return $items;


$mysql_close($link);


	}



else if($action== 'SMS-NOTIFICATION')
{
list($action, $messagetype, $source, $type) =explode ("

",$list);
}

return $list;


}
}



?>

This will save all the messages into a database repository

My problem is it did not work for me.
Please help me fix this problem?

Please do 2 things so you can get the help you need.

Please stop starting new threads about the same problem, it doesn’t help others to see what you’ve already tried or what suggestions you’ve already been given.

Instead of saying “It doesn’t work” please give more detail about how it’s not working and any error messages you’re getting with error reporting on.

Off Topic:

How old is this code? It’s using deprecated mysql_

It does not work cause no response from my server but there is no error message when run in www.codepad.org.
And i apologize for having another post even if the first is not yet solve.

Well, that is my problem connecting to mysql. The original code was written above i only add my code for connecting to mysql database.
It’s from last year code

OK, thanks. So let’s put aside the “receive” code for now and focus on the “send” code.

You have

 <?php

$globe = new GlobeApi('v1');
$sms = $globe->sms([87258]);
$response = $sms->sendMessage([access_token], [number], [message]);

Array (
  "outboundSMSMessageRequest" => Array (
    "address" => "0915xxxxxxx",
    "deliveryInfoList" => Array (
      "deliveryInfo" => [],
      "resourceURL" => null
    ),
    "senderAddress" => "21587",
    "outboundSMSTextMessage" => Array (
      "message": " Diagnose with  "
    ),
    "receiptRequest" => Array (
      "notifyURL" => null,
      "callbackData" => null,
      "senderName" => null,
      "resourceURL" => null
    )
  )
)
?> 

which doesn’t look much like http://codecri.me/case/324/sending-sms-text-messages-via-the-globe-labs-sms-api/

<?php
$data = array();
$data['post'] = array(
   'uName' => $username,
   'uPin'  => $pin,
   'MSISDN' => '09277728743',
   'messageString' => 'SMS sent via Globe labs API',
   'Display' => '1',
   'udh' => '',
   'mwi' => '',
   'coding' => '0',
);
$response = xhttp::fetch('http://iplaypen.globelabs.com.ph:1881/axis2/services/Platform/sendSMS', $data);

if($response['successful']) {
   preg_match('/<ns:return>([0-9]+)<\\/ns:return>/', $response['body'], $matches);
   $code = $matches[1];
   if($code == '201') {
      echo 'SMS accepted for delivery!';
   } else {
      echo 'Error occured! Error code: '.$code;
   }
}
?>

Is this the API you’re using? If not, what one are you using? If so, have you been “granted a shortcode + suffix specific for your application, plus an API username and pin for authenticating requests.”?

No. i am using developer.globelabs.com.ph with short code ‘2158728’ and i have my API id key…the one that you’ve mention is deprecated SOAP. We are using the new REST API php
Using Globe API

Thanks, now we’re getting somewhere. https://github.com/globelabs/api/blob/master/PHP/src/Sms.php

<?php
class Sms extends GlobeApi
{
    //PUBLIC VARIABLES
    public $version;
    public $recepient;
    public $message;
    public $description;
    public $address;
    public $senderName;
    public $clientCorrelator;
    public $criteria;
    public $subscriptionId;
    public $accessToken;
.....
    /**
* triggers the send or the message
*
* @param string|null $accesstoken the access token of the user to be charged
* @param string|null $number the number of the user to be charged
* @param string|null $message the message to be sent
* @param boolean $bodyOnly returns the header if set to false
* @return array
*/
    public function sendMessage(
        $accesstoken = null,
        $number = null,
        $message = null,
        $bodyOnly = true
    ) {
        if($accesstoken) {
            $this->accessToken = $accesstoken;
        }

        if($number) {
            $this->recepient = $number;
        }

        if($message) {
            $this->message = $message;
        }

        if(!$this->recepient) {
            throw new Exception('recepient is required');
        }

        if(!$this->message) {
            throw new Exception('message is required');
        }

        if(!$this->address) {
            throw new Exception('shortcode is required');
        }

        $url = sprintf(
            Sms::CURL_URL,
            GlobeAPI::API_ENDPOINT,
            $this->version,
            urlencode($this->address)
        );

        $url.='requests';
        $format = "";

        $postFields = array(
            'access_token' => $this->accessToken,
            'address' => $this->recepient,
            'message' => $this->message,
            'senderAddress' => $this->address,
            'description' => $this->description,
            'clientCorrelator' => $this->clientCorrelator
        );

        $postFields = array_filter($postFields);

        $response = $this->_curlPost($url, $postFields);

        return $this->getReturn($response, $bodyOnly);
    }
}

I’m confused about this, but it doesn’t look right.

Array (
  "outboundSMSMessageRequest" => Array (
    "address" => "0915xxxxxxx",
    "deliveryInfoList" => Array (
      "deliveryInfo" => [],
      "resourceURL" => null
    ),
    "senderAddress" => "21587",
    "outboundSMSTextMessage" => Array (
      "message": " Diagnose with  "
    ),
    "receiptRequest" => Array (
      "notifyURL" => null,
      "callbackData" => null,
      "senderName" => null,
      "resourceURL" => null
    )
  )
)

If you try

 <?php

$globe = new GlobeApi('v1');
$sms = $globe->sms([87258]);
$response = $sms->sendMessage([access_token], [number], [message]);
var_dump($response);
?>

do you see anything helpful?

It was just given to me by Globe Labs re: sending sms via restFul API.

Which one is correct?
Im confused…

If you got the code from them hopefully they know more than I do :wink:

I’ve copied the files from Github and will study them ASAP and get back.

I’ve had a look at things.

The “Array (” stuff doesn’t belong in your code. That is shown in the docs as an example of what the response JSON looks like.

I think the first thing you should try is this found in the test folder.

<?php
    session_start();
    require ('api/PHP/src/GlobeApi.php');
    $globe = new GlobeApi();
    $auth = $globe->auth(
        [KEY],
        [SECRET]
    );
    
    if(!isset($_SESSION['code'])) {
        $loginUrl = $auth->getLoginUrl();
        header('Location: '.$loginUrl); 
        exit;
    }
    
    if(!isset($_SESSION['access_token'])) {
        $response = $auth->getAccessToken($_SESSION['code']);
        $_SESSION['access_token'] = $response['access_token'];
        $_SESSION['subscriber_number'] = $response['subscriber_number'];
    }

    $sms = $globe->sms(5286);
    $response = $sms->sendMessage($_SESSION['access_token'], $_SESSION['subscriber_number'], 'rakers api');

    $charge = $globe->payment(
        $_SESSION['access_token'],
        $_SESSION['subscriber_number']
    );

    $response = $charge->charge(
        0,
        '52861000001'
    );
?>

For some reason the author is using brackets to indicate things that need to be replaced, when you replace KEY and SECRET -do not- enclose the values in brackets.
You may need to change your require() path.
The docs sms.md file also says
“You also need to remove the 2158 digit in your short code.” though I think that will come later and not with this

Ok thanks.I will do your suggestion.
Noted.

My professor have change my topic research and it’s now virtual companion using Android that aims to interact with the user. I have the idea but the problem is how to implement this into a system.
The objective is to have the user, a virtual friend who can talk with as a companion in everyday life.

Sir i am new about this… and as i try changing the things needed to be change regarding the code (example: Key and Secret). I manage to receive a notification from globelabs in my phone (i grant the number from my app in globelabs), but then i still cant figure out how to send sms using my own webpage (example message: “hello there”). can you help me out sir?

Hi Guys,

I hope I’m not hi-jacking this thread. My concern is related to above API that’s why I decided to post my question here.

This is my first time to create an application/webpage based on PHP. I already got past the registration (authentication) of mobile numbers and sending an SMS reply. To those having problems with these steps, you may use the test.php included. Just need to add a line after the declaration of App ID and App Key in order to set the session’s code. Check the auth.php for the function of setting the code. (Sorry can’t provide exact code I used as I’m using a public pc and can’t access github. And I’m referring to post# 11)

My main concern now is how to receive/process the notifications that are supposed to be sent to my notification url. I tried using below code to retrieve data but I’m unsure why I’m not getting anything.

<?php
$json = file_get_contents(‘php://input’);
$json = stripslashes($json);
$values = json_decode($json, true);
?>

I tried to echo, print_r and output to a text file the $values variable but they all return nothing. I even thought that the api itself may not be working but an API engineer of the service provider asked me to used a pre-defined notify url that they use for testing and it works. He also mentioned that there’s nothing wrong with my code snippet.

Maybe my question now is, (1) is there a better alternative to retrieve the said notification. (2) I’m using IP format (http://xxx.xxx.xxx.xxx:/8081/notification.php) for the notification url, does it affect data retrieving?. (3) I’m also new to web applications and just started using IIS. Is there anything I should check in the settings to ensure that data is getting passed to my notification url?

Thanks in advance.

It seems like no one has been visiting this post anymore. But just in case someone comes here and needs help with the Globe SMS api, my above code is actually working for incoming sms. It just so happen that the API does not support my 8081 port number. As soon as I used the default port 80, I was able to make the code work.

For receiving messages and authenticating mobile numbers, just follow my previous tip.