[Solved] XMLHTTP post |
Author: |
Message: |
Deco
Full Member
Posts: 188 Reputation: 4
42 / /
Joined: Aug 2006
|
O.P. [Solved] XMLHTTP post
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!
This post was edited on 08-29-2007 at 04:56 PM by Deco.
|
|
08-28-2007 09:12 PM |
|
|
roflmao456
Skinning Contest Winner
Posts: 955 Reputation: 24
30 / /
Joined: Nov 2006
Status: Away
|
RE: XMLHTTP post
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()
This post was edited on 08-29-2007 at 12:26 AM by roflmao456.
[quote]
Ultimatess6: What a noob mod
|
|
08-29-2007 12:20 AM |
|
|
Deco
Full Member
Posts: 188 Reputation: 4
42 / /
Joined: Aug 2006
|
O.P. RE: XMLHTTP post
Thanks
|
|
08-29-2007 01:16 AM |
|
|
-dt-
Scripting Contest Winner
;o
Posts: 1819 Reputation: 74
36 / /
Joined: Mar 2004
|
RE: XMLHTTP post
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))
Happy Birthday, WDZ
|
|
08-29-2007 01:54 AM |
|
|
davidpolitis
Full Member
Posts: 371 Reputation: 16
Joined: Aug 2006
|
RE: RE: XMLHTTP post
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
[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.");
}
}
?>
This post was edited on 08-29-2007 at 10:20 AM by davidpolitis.
|
|
08-29-2007 10:15 AM |
|
|
Deco
Full Member
Posts: 188 Reputation: 4
42 / /
Joined: Aug 2006
|
O.P. RE: XMLHTTP post
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.
|
|
08-29-2007 03:08 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: XMLHTTP post
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);
}
|
|
08-29-2007 03:39 PM |
|
|
Deco
Full Member
Posts: 188 Reputation: 4
42 / /
Joined: Aug 2006
|
O.P. RE: XMLHTTP post
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!
|
|
08-29-2007 04:16 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: XMLHTTP post
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!');
}
This post was edited on 08-29-2007 at 04:41 PM by Matti.
|
|
08-29-2007 04:38 PM |
|
|
Deco
Full Member
Posts: 188 Reputation: 4
42 / /
Joined: Aug 2006
|
O.P. RE: XMLHTTP post
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.
|
|
08-29-2007 04:43 PM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|