AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Read XML files (https://forums.alliedmods.net/showthread.php?t=183316)

miki208 04-20-2012 13:28

Read XML files
 
Is this possible?
Please, help me, I need it... :)

claudiuhks 04-20-2012 14:08

Re: Read XML files
 
If you want to read a file, with XML extension, it's possible. It's also possible to read a file with OINFNDW extension if you want.

PHP Code:

new iFile fopen"MyFile.MyExtension""r" ), iReadRowscMyRow256 ][ 32 ];
while( !
feofiFile ) ) fgetsiFilecMyRowiReadRows ], 31 ), iReadRows++;
fcloseiFile ); 

Be careful, a file which not exists and it's opened will crash server.

fysiks 04-21-2012 00:57

Re: Read XML files
 
You can read any plain text file (or even binary files if you know what you are doing) but there is nothing that is default that will actually parse a xml content. Look in the Code Snippets/Tutorials to see if it's been done. Also, search in the Scripting Help forum. If you can't find it then it's likely that you will have to create it. However, I don't expect that it would be an easy task unless you are only looking for a single value and you don't want all the information in the file.

Quote:

Originally Posted by claudiuhks (Post 1693083)
Be careful, a file which not exists and it's opened will crash server.

Or better yet, you should put the check in the code.

miki208 04-21-2012 11:27

Re: Read XML files
 
But if I want to parse it with PHP script, how to send request to this PHP script and get returned data from PHP?
Someone tell me that is possible with socket, but I don't know how, please write me an example how to send request and get data. :/

fysiks 04-21-2012 13:10

Re: Read XML files
 
You have to use sockets to send/receive info from a webpage (your php script). There are many examples of this because it's a popular request. Also, check in the tutorials forum and see if there is a sockets tutorial.

miki208 04-22-2012 06:24

Re: Read XML files
 
socket_open ( hostname[], port, protocol, &error )
Can u write me an example, how to connect, if my php file is here: www.somesite.com/myfile.php
Which arguments will be in this functon if I want to use socket with this php file?
Sry for bad english :/
EDIT: this site and php file doesn't exists, i want code for connecting socket with this file, based on this example...

fysiks 04-22-2012 14:26

Re: Read XML files
 
Look at examples.

miki208 04-22-2012 14:58

Re: Read XML files
 
PHP Code:

#include <amxmodx> 
#include <sockets> 

#define PLUGIN    "sdfds" 
#define AUTHOR    "fsdsddfs" 
#define VERSION    "1.0" 

// Tasks 
#define TASKID_GETANSWER            0 
#define TASKID_CLOSECONNECTION        1 


new g_Socket 

// Data buffer 
new g_Data[1000
public 
plugin_init() 

    
register_plugin(PLUGINVERSIONAUTHOR


public 
plugin_cfg() 

    
// Connect 
    
new errorsendbuffer[512
    
g_Socket socket_open("mikisoft.clanteam.com"80SOCKET_TCPerror
  
     
    
// Send page request 
    
format(sendbuffer511"GET /prognoza.php HTTP/1.1^nHost:mikisoft.clanteam.com^r^n^r^n"
    
socket_send(g_Socketsendbuffer511
     
     
    
// Setting the tasks for handeling the response 
    
set_task(1.0"task_waitanswer"TASKID_GETANSWER""0"a"15
    
set_task(16.0"task_closeconnection"TASKID_CLOSECONNECTION""0""0
}

public 
task_waitanswer(id

    
// It changed? 
    
if (socket_change(g_Socket)) 
    { 
        
// Get the data 
        
socket_recv(g_Socketg_Data999)  
        
socket_close(g_Socket
             
            
// Remove the tasks 
            
remove_task(TASKID_GETANSWER
            
remove_task(TASKID_CLOSECONNECTION
        
client_print(id,print_chat,g_Data)
    } 


public 
task_closeconnection(id

    
// Close connection if no response in 16 seconds. 
    
socket_close(g_Socket


this doesn't work. :/
I must to get answer from server: 'pozzz' :/
I get this answer:
PHP Code:



HTTP
/1.1 200 OK
Date
Sun22 Apr 2012 19:13:39 GMT
Server
Apache
X
-Powered-ByPHP/5.2.17
Content
-Length:
6
Connection


PHP script 'prognoza.php':
PHP Code:

<?php
    
echo "pozzzz";
?>


Exolent[jNr] 04-24-2012 10:36

Re: Read XML files
 
PHP Code:

public task_waitanswer(id

    
// It changed? 
    
if (socket_change(g_Socket)) 
    { 
        
// Get the data 
        
socket_recv(g_Socketg_Data999)  
        
socket_close(g_Socket
        
        
// Remove the header from the data
        
new pos contain(g_Data"^r^n^r^n")
        if(
pos 0) {
                
format(g_Data999g_Data[pos 2])
        }
             
            
// Remove the tasks 
            
remove_task(TASKID_GETANSWER
            
remove_task(TASKID_CLOSECONNECTION
        
client_print(id,print_chat,g_Data)
    } 



miki208 04-25-2012 18:20

Re: Read XML files
 
Tnx you man :)
I made plugin 'weather'. When u type /weather city_name, socket must to connect and send data to php script and get answer. That's work. I tested my plugin on server which is hosted on my computer. And that's work only when i type /weather city, but other players can't. Is it possible that sockets only can to connect from my computer, only from IP adress of my server, and how to resolve this problem? :/
PHP Code:

//plugin_init
register_concmd("say","s",0,"plugin");
//s function is called when someone activate say command, and check is it starts with '/weather', and connect socket
//i note that only I can to can to call this function, but i want to all players can
public s(id

    
// Connect 
    
new DATA[1000];
    new 
komanda[50];
    
read_argv(id,komanda,sizeof(komanda));
    new 
naredba[50];
    
strtok(komanda,naredba,49,argument,49,' ');
    
replace_all(argument,49," ","%20");
    if(
contain(komanda,"/weather")==0)
    {
    new 
errorsendbuffer[512
    
g_Socket socket_open("serbia-chat.net16.net"80SOCKET_TCPerror
  
     
    
// Send page request 
    
format(sendbuffer511"GET /prognoza.php?grad=%s HTTP/1.1^nHost:serbia-chat.net16.net^r^n^r^n",argument
    
socket_send(g_Socketsendbuffer511
     
     
    
// Setting the tasks for handeling the response 
    
set_task(1.0"task_waitanswer"TASKID_GETANSWER""0"a"15
    
set_task(16.0"task_closeconnection"TASKID_CLOSECONNECTION""0""0
   }
   




All times are GMT -4. The time now is 07:41.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.