Shoutbox

[Solved] XMLHTTP post - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [Solved] XMLHTTP post (/showthread.php?tid=77072)

[Solved] XMLHTTP post by Deco on 08-28-2007 at 09:12 PM

Hi guys

I've seen examples on how to use xmlhttp to get and post info in a page.

Could anyone send me an example of a php page that would accept values such as my status and my username so someone else can run the script and fetch it?

Purpose of the script I want to make:

I stay apearing offline most of the time but I want to make a script to install at my girlfriend's computer so she can tell if I'm appearing offline or really offline. I thought I could use a script to post in my website and then she can have the script to retrieve it. Any other ways other than blockingn everybody and leaving only her unblocked?

Thanks!


RE: XMLHTTP post by roflmao456 on 08-29-2007 at 12:20 AM

basic, but try this:

code:
<?php
if($_POST['status']){
file_put_contents("currentstatus.txt",$_POST['status']); // PHP 5 required for this. otherwise use "fclose(fwrite(fopen("currentstatus.txt","w"),$_POST['status']));"
// done..
}
?>

code:
var isOnline = false;
function getInfo(){
Interop.Call('wininet.dll', 'DeleteUrlCacheEntryW', "http://www.wtfbbq.com/currentstatus.txt");
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","http://www.wtfbbq.com/currentstatus.txt",true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.responseText == "2" && isOnline == false){
MsgPlus.DisplayToast("","thisperson is appearing offline. you can talk to him/her");
isOnline = true;
} else {
isOnline = false;
}
}
}
xmlhttp.send();
}

code:
function postInfo(status){
/*
Usage: postInfo(
[int] status
);

Status Values
2 - Appear Offline
3 - Online
4 - Busy
5 - Be Right Back
6 - Idle
7 - Away
8 - In a Call
9 - Out to Lunch
0 - Unknown
*/
Interop.Call('wininet.dll', 'DeleteUrlCacheEntryW', "http://www.wtfbbq.com/status.php");
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST","http://www.wtfbbq.com/status.php",true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
Debug.Trace("Sent");
}
}
xmlhttp.setRequestHeader("status",status);
xmlhttp.send();
}

function OnEvent_Signout(){
postInfo(0);
}

function OnEvent_MyStatusChange(newstatus){
postInfo(newstatus);
}

just create a timer for the getInfo() :P
RE: XMLHTTP post by Deco on 08-29-2007 at 01:16 AM

Thanks


RE: XMLHTTP post by -dt- on 08-29-2007 at 01:54 AM

quote:
Originally posted by roflmao456
xmlhttp.setRequestHeader("status",status);
xmlhttp.send();
er you're susposed to send the POST string as the param to send();
setRequestHeader sets http headers, like user-agent and Content-type

so you should do something like
code:
xmlhttp.send("status=" + encodeURIComponent(status))


RE: RE: XMLHTTP post by davidpolitis on 08-29-2007 at 10:15 AM

quote:
Originally posted by roflmao456
code:
<?php
if($_POST['status']){
file_put_contents("currentstatus.txt",$_POST['status']); // PHP 5 required for this. otherwise use "fclose(fwrite(fopen("currentstatus.txt","w"),$_POST['status']));"
// done..
}
?>

I personally prefer the code below created by me :P

code:
<?php

if($_POST['status'])
{
if($_SERVER['REMOTE_ADDR'] == "IP ADDRESS HERE")
{
  $file = fopen("status.txt", "w");

  fwrite($file, $_POST['status']);

  fclose($file);
}
else
{
  echo("Sorry, you are not permitted to view this page.");
}
}

?>

RE: XMLHTTP post by Deco on 08-29-2007 at 03:08 PM

code:
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var LastStatus = "offline";
var FirstTime = true;
var SignOut = false;
xmlhttp.onreadystatechange = function() {
    Debug.trace("xmlttp function");
    if(xmlhttp.readyState==4) {
        if(xmlhttp.status == 200){
            Debug.Trace("Received info-> " + xmlhttp.responseText);
            ParseResponse(xmlhttp.responseText);
        }else{
            Debug.trace("Post failed!");
        }
    }
}

function OnEvent_SignIn(email) {
PostInfo('online');
MsgPlus.AddTimer("verificar",120000);
}
function OnEvent_SignOut(email) {
SignOut = true;
PostInfo('offline');
}
function PostInfo(status) {
Interop.Call('wininet.dll', 'DeleteUrlCacheEntryW', "http://www.mysite.com/statusmsn.php");
xmlhttp.open("POST", "http://www.mysite.com/statusmsn.php", true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var str = "user=" + Messenger.MyEmail + "&status=" + status;
Debug.trace("Posted info-> " + str);
xmlhttp.send(str);   
}

function ParseResponse(strText) {
    Debug.trace("Parsing...");
    if (!SignOut) {
    var index = strText.indexOf(" ");
    if (index > 0) {
        strName = strText.substr(0,index);
        strStatus = strText.substr(index+1);
        Debug.trace("Parse-> " + strName + " " + strStatus);
        if(strName == "heremail@hotmail.com") {
            if (strStatus == "online") {
                if (LastStatus == "offline" || FirstTime) {
                    MsgPlus.DisplayToast("Toast", "girlfriend está online!\nClique aqui para abrir um chat.", "","OnMyToastClick", strName);
                    FirstTime = false;
                    LastStatus = "online";
                }
            } else {
                if (LastStatus == "online"|| FirstTime ) {
                    MsgPlus.DisplayToast("Toast", "Deco's girlfriend está offline...", "","");
                    LastStatus = "offline";
                }
            }
        } else {
            if (strStatus == "online") {
                if(LastStatus == "offline"|| FirstTime) {
                MsgPlus.DisplayToast("toast", "Deco está online!\nClique aqui para abrir um chat.", "","OnMyToastClick",strName);
                FirstTime = false;
                LastStatus = "online";
                }
            } else {
                if (LastStatus == "online"|| FirstTime) {
                    MsgPlus.DisplayToast("Toast", "Deco está offline...", "","");
                    LastStatus = "offline";
                }
            }
        }
    }
    } else {
    Debug.trace("Singing out.");
    }
}

function OnMyToastClick(Param)
{
    var tmpContact = Messenger.MyContacts.GetContact(Param);
    Messenger.OpenChat(Param);
}

function OnEvent_Timer(tID) {
    if (tID == "verificar") {
        PostInfo('online');
        Debug.trace("Checando informação.");
        MsgPlus.AddTimer("verificar",120000)
    }
}


Updates the info in the server just right. Problem is that when the timer comes in and it posts an update.. it won't activate the .onreadystatechange function. Why?

Thanks for all the people that helped me in this thread and by PMs.


RE: XMLHTTP post by matty on 08-29-2007 at 03:39 PM

Change the PostInfo function to this:

code:
function PostInfo(status) {
    Interop.Call('wininet.dll', 'DeleteUrlCacheEntryW', 'http://www.mysite.com/statusmsn.php');
   
    var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    xmlhttp.onreadystatechange = function() {
        Debug.trace('xmlttp function');
        if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            Debug.Trace('Received info-> ' + xmlhttp.responseText);
            ParseResponse(xmlhttp.responseText);
        }else{
            Debug.trace('Post failed!');
        }
    }
   
    xmlhttp.open('POST', 'http://www.mysite.com/statusmsn.php', true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    var str = 'user=' + Messenger.MyEmail + '&status=' + status;
    Debug.trace('Posted info-> ' + str);
    xmlhttp.send(str);
}

RE: XMLHTTP post by Deco on 08-29-2007 at 04:16 PM

Changed... Debug window gives me this:

Function called: OnEvent_Signin
Post failed!
Posted info-> user=decosemail@email.com&status=online
Post failed!
Post failed!
Post failed!
Received info-> heremail@hotmail.com offline
Parsing...
Parse-> heremail@hotmail.com offline

Are these failed posts supposed to be there?

Thanks!


RE: XMLHTTP post by Matti on 08-29-2007 at 04:38 PM

Well, this is just because onreadystatechange actually gets called multiple times by the object. The XmlHttp object will tell the script what's it doing, so you can (eventually) make some sort of "status bar" for your script, but I don't think it's needed to use a GUI for this and thus it's not important. Therefore, you only need to know when it's done, and that's where readyState == 4 is for.

A reference of the readyState property explains why you get this:

quote:
Originally posted by DevGuru XmlHttp object reference
The readyState property indicates the current state of the request. It returns a 4-byte integer.

This property is read-only and has the following defined values

    * UNINITIALIZED(0)
      The object has been created, but has not been initialized (the open method has not been called).
    * LOADING(1)
      The object has been created but the send method has not been called.
    * LOADED(2)
      The send method has been called and the status and headers are available, but the response is not.
    * INTERACTIVE(3)
      some data has been received. You can get the partial results using the responseBody and the responseText properties.
    * COMPLETED(4)
      All the data has been received, and is available.
If you compare this to your debug, you'll understand what's happening:
quote:
Function called: OnEvent_Signin
Post failed! - Uninitialized, you didn't call open() yet.
Posted info-> user=decosemail@email.com&status=online
Post failed! - Loading, you didn't call send() yet.
Post failed! - Loaded, the response is not available yet.
Post failed! - Interactive, some data has already been retrieved.
Received info-> heremail@hotmail.com offline - Completed, so you're function takes over as you can see.
Parsing...
Parse-> heremail@hotmail.com offline

My suggestion: leave out the else block. If you want to check for errors, use this instead:
code:
        if(xmlhttp.readyState === 4) {
            if(xmlhttp.status === 200) {
               Debug.Trace('Received info-> ' + xmlhttp.responseText);
               ParseResponse(xmlhttp.responseText);
            } else Debug.Trace('Post failed!');
        }

RE: XMLHTTP post by Deco on 08-29-2007 at 04:43 PM

Great explanation. I knew something like this was happening but didn't know where to find the info.

It's working. Thanks a lot guys.


RE: [Solved] XMLHTTP post by roflmao456 on 08-29-2007 at 05:33 PM

quote:
Originally posted by davidpolitis
I personally prefer the code below created by me :P

but IPs change, don't they o.O