I'm using a simple MQTT client in PHP - https://github.com/sskaje/mqtt - and want to retrieve exactly one message (which is retained - always there) from broker, and then display it on a page. Everything works good, but I cannot get it to display the whole page. It displays "Test Text 1", then debugging code, then my message, and stops there, not showing "Test Text 2" nor "Test Text 3". If anyone would help me, I would be incredibly grateful, as I have no idea at all, what does not work, and have spent a lot of time working on it. Thanks!
Test Text 1
<?php
require('spMQTT.class.php');
$mqtt = new spMQTT('tcp://127.0.0.1:1883/');
spMQTTDebug::Enable();
$mqtt->setKeepalive(5);
$connected = $mqtt->connect();
if (!$connected) {
die("Not connected\n");
}
$topics['#'] = 0;
$mqtt->subscribe($topics);
$mqtt->loop('default_subscribe_callback');
$mqtt->unsubscribe(array_keys($topics));
printf("Test Text 2");
/**
* #param spMQTT $mqtt
* #param string $topic
* #param string $message
*/
function default_subscribe_callback($mqtt, $topic, $message) {
printf("Message received: Topic=%s, Message=%s\n", $topic, $message);
break;
}
?>
Test Text 3
I'm not familiar with that mqtt library, but it looks very much like the call to loop() is blocking, so you will have to approach the problem differently.
Related
Good day, I would like to create a menu for my ussd app, it's my first app so i'm still a little bit confused.
The only i was able to do so far is to display message on the screen that says "Welcome dear customer", but me I want to create an interactive menu.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header("Content-type: text/xml; charset=utf-8");
/**
* Here we receive the xml from the network company
*
*/
$json = file_get_contents('php://input');
$json_data = json_decode($json);
error_log($json_data->param1);
$xml = simplexml_load_string($json_data->param1) or die('Error:
Cannot create the object');
$MessageType = $xml->MessageType;
$ConversationID = $xml->ConversationID;
$SessionID = $xml->SessionID;
$TransactionID = $xml->TransactionID;
$MessageString = $xml->MessageString;
$MSISDN = $xml->MSISDN;
$Success = $xml->Success;
$IsFinal = $xml->IsFinal;
$DateTimeReceived = $xml->DateTimeReceived;
$MNO = $xml->MNO;
/**
*Here we create a xml that will send a message to the user
*
*
*/
$option1 ='1.Welcome dear customer';
$xml = new SimpleXMLElement('<UssdMessage/>');
$xml->addChild('MessageType', $MessageType);
$xml->addChild('ConversationID', $ConversationID);
$xml->addChild('SessionID', $SessionID);
$xml->addChild('TransactionID', $TransactionID);
$xml->addChild('MessageString', $option1);
$xml->addChild('MSISDN', $MSISDN);
$xml->addChild('Success', 'true');
$xml->addChild('IsFinal', 'TRUE');
$xml->addChild('DateTimeReceived', $DateTimeReceived);
$xml->addChild('MNO', $MNO);
echo $xml->asXML();
The platform that converts the XML to SMPP must return some type of session indicator and with the MSISDN of the subscriber you can respond to the options that the user has selected.
We implement a finite state machine to support the logic of the menu.
Each operation is a state and in its definition you link to the next state in case of transition OK or in case of transition ERROR you respond a message to the user.
Within the SMPP protocol, the ussd_service_op parameter is usually used to determine if the response sent allows the user to respond or is a response that ends a user session.
The provider of the platform should give you the documentation referring to the bidirectional sessions and there should be the values for ussd_service_op.
I leave the link to the documentation of the SMPP protocol, you should read about submit_sm and deliver_sm http://docs.nimta.com/SMPP_v3_4_Issue1_2.pdf.
Sorry for my foreign english
I am subscribing to data from a MQTT broker with phpMQTT. I have successfully set up a pub / sub routine based on their basic implementation. I can echo the information just fine inside the procmsg() function.
However, I need to take the data I receive and use it for running a few database operations and such. I can't seem to get access to the topic or msg received outside of the procmsg() function. Using return as below seems to yield nothing.
<?php
function procmsg($topic, $msg){
$value = $msg * 10;
return $value;
}
echo procmsg($topic, $msg);
echo $value;
?>
Obviously I am doing something wrong - but how do I get at the values so I can use them outside the procmsg()? Thanks a lot.
I dont know about that lib, but in that code
https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php ,
its possible see how works.
in :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"procmsg");
you are telling it that topic "edafdff398fb22847a2f98a15ca3186e/#" will have Quality of Service (qos) = 0, and an "event" called 'procmsg'.
That's why you later wrote this
function procmsg($topic,$msg){ ... }
so in the while($mqtt->proc()) this function will check everytime if has a new message (line 332 calls a message function and then that make a call to procmsg of Source Code)
thats are the reason why you cannot call in your code to procmsg
in other words maybe inside the procmsg you can call the functions to process message ej :
function procmsg($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Note that you can change the name of the function simply ej :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"onMessage");
and then :
function onMessage($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Sorry for my english, hope this help !
I get the following error:
Catchable fatal error: Argument 1 passed to CorenlpAdapter::getOutput() must be an instance of string, string given, called in /Library/WebServer/Documents/website/php-stanford-corenlp-adapter/index.php on line 22 and defined in /Library/WebServer/Documents/website/php-stanford-corenlp-adapter/src/CoreNLP/CorenlpAdapter.php on line 95
index.php 21 and 22 contain:
$text1 = 'I will meet Mary in New York at 10pm';
$coreNLP->getOutput($text1);
corenlpAdapter.php lines 95 and onwards contain:
public function getOutput(string $text){
if(ONLINE_API){
// run the text through the public API
$this->getServerOutputOnline($text);
} else{
// run the text through Java CoreNLP
$this->getServerOutput($text);
}
// cache result
$this->serverMemory[] = $this->serverOutput;
if(empty($this->serverOutput)){
echo '** ERROR: No output from the CoreNLP Server **<br />
- Check if the CoreNLP server is running. Start the CoreNLP server if necessary<br />
- Check if the port you are using (probably port 9000) is not blocked by another program<br />';
die;
}
/**
* create trees
*/
$sentences = $this->serverOutput['sentences'];
foreach($this->serverOutput['sentences'] as $sentence){
$tree = $this->getTreeWithTokens($sentence); // gets one tree
$this->trees[] = $tree; // collect all trees
}
/**
* add OpenIE data
*/
$this->addOpenIE();
// to get the trees just call $coreNLP->trees in the main program
return;
}
Why exactly am I getting this error when text1 is a string?
I am the original author of this class. As you can see, the function getOutput looks like this:
public function getOutput(string $text){
...
}
Change that to:
public function getOutput($text){
...
}
The function tries to enforce that the input is string. The original code should work. However, it seems that in your case, PHP thinks "string" is not actually a string. Maybe the coding environment (the IDE) you are using uses the wrong character set? Or maybe you copy-pasted the code from HTML into the IDE or something like that. Thus whilst it says "string" on the screen, it's not actually a string to PHP.
If you are sure the input is a string, you can safely change the code like above. The class should then work normally.
public function getOutput($text){
.
.
.
}
I recently swithced php version from 4 to 5.3. I now how some code that is not working anymore. I have a PHP script that fetches data from a form into a new form, when a user clicks a link.
First it recognizes the user/account and after that it finds the form data.
This is the code for the account data:
$account_info = ft_get_account_info($_SESSION["ft"]["account"]["account_id"]);
$emailadresse = ($account_info['email']);
$accountid = ($account_info['account_id']);
$firstname =($account_info['first_name']);
$lastname =($account_info['last_name']);
....
....
This works, and i can display the data through for example a:
<?php echo $_POST['firstname']; ?>
I then have this code in order to fetch and display the form data:
$submission_info = ft_get_submission_info($form_id, $submission_id);
$submission_id = ($submission_info['submission_id']);
$partname = ($submission_info['partname']);
$ponumber = ($submission_info['ponumber']);
....
....
<?php echo $_POST['partname']; ?>
This is not working anymore in version 5.3 of PHP.
Can anyone please tell what i need to re-write this code into, in order for it to work...????
Thanks in advance!
In addition to the comments i have this code for the ft_get_account_info:
$_SESSION["ft"]["account"] = ft_get_account_info($account_info["account_id"]);
And this for the ft_get_submission_info:
/**
* Returns all information about a submission. N.B. Would have been nice to have made this just a
* wrapper for ft_get_submission_info, but that function contains hooks. Need to revise all core
* code to allow external calls to optionally avoid any hook calls.
*
* #param integer $form_id
* #param integer $submission_id
*/
function ft_api_get_submission($form_id, $submission_id)
{
global $g_table_prefix, $g_api_debug;
// confirm the form is valid
if (!ft_check_form_exists($form_id))
{
if ($g_api_debug)
{
$page_vars = array("message_type" => "error", "error_code" => 405, "error_type" => "user");
ft_display_page("../../global/smarty/messages.tpl", $page_vars);
exit;
}
else
return array(false, 405);
}
if (!is_numeric($submission_id))
{
if ($g_api_debug)
{
$page_vars = array("message_type" => "error", "error_code" => 406, "error_type" => "user");
ft_display_page("../../global/smarty/messages.tpl", $page_vars);
exit;
}
else
return array(false, 406);
}
// get the form submission info
$submission_info = mysql_query("
SELECT *
FROM {$g_table_prefix}form_{$form_id}
WHERE submission_id = $submission_id
");
$submission = mysql_fetch_assoc($submission_info);
return $submission;
}
Nothing on the error reporting.
I don't know what your specific issue is, but I figure that if you use all the tools available to you for debugging, you'll be able to find your issue easily.
For development, you should always up your error reporting level to E_ALL ^ E_STRICT. You can find this setting in your php.ini file. E_STRICT specifically will help identify interoperability and compatibility issues, and is not included in E_ALL until PHP 5.4, according to the manual.
You may also want to use Netbeans and XDebug, which should allow you to step through your code line by line, which will simplify debugging immensely. There is a guide for setting up these tools here: Debugging PHP Source Code in the NetBeans IDE
I am pulling down an email which has english, chinese and japanese in the email.
I was using PHP/EZComponents to do this, but a certain japanese char was just not coming through so I am switching to php imap_* funcs to see if they will work.
This is what I have below, and the output I am getting. I need to decode this somehow... I know this has been well (read:overly/chaotically) documented all over the web, but I dont have time to earn a PHD in this right now. Any help is greatly appreciated.
$hn='{imap.gmail.com:993/imap/ssl}INBOX';
$inbox = imap_open($hn,$username,$password,CL_EXPUNGE);
foreach($emails as $email_number) {
$ov = imap_fetch_overview($inbox,$email_number,0);
$msg = imap_fetchbody($inbox,$email_number,2);
var_dump($msg);
// doesnt work... .. but right idea?
// var_dump( utf8_decode($msg) );
}
PARTIAL OUTPUT:
<font face=3D"Arial"><span lang=3D"EN-US" style=3D"font-size:10.5pt"><br></=
span></font><font color=3D"navy" face=3D"MS Gothic"><span lang=3D"JA" style=
=3D"font-size:10.5pt">=CC=EC=9A=DD=A4=AC=A4=A4=A4=A4=A4=AB=A4=E9=A1=A2</spa=
n></font></p><p style=3D"margin-right:0pt;margin-bottom:12pt;margin-left:0p=
t">
<font color=3D"navy" face=3D"MS Gothic"><span lang=3D"JA" style=3D"font-siz=
e:10.5pt"><br></span></font></p><p style=3D"margin-right:0pt;margin-bottom:=
12pt;margin-left:0pt"><font color=3D"navy" face=3D"MS Gothic"><span lang=3D=
"JA" style=3D"font-size:10.5pt">xxend</span></font></p>
I also met this problem employed imap_fetchbody function to get the mail body.
I found that the string get from imap_fetchbody was converted to quoted-printable string automatically.
I resolved this issue by using imap_qprint function to convert the fetched string body into correct one.