LINUX.ORG.RU

Запостить сообщение на сайте?

 ,


0

1

Использую libcurl, нужно на сайте один постит сообщение в тег

<p> message=#%some_number%,%some_message%</p>


другой читает и наоборот если номер сообщения уже есть в textedit'e то не добавляется и так каждую секунду проверяет

Как говорит один тип «Всем всё нужно».

pon4ik ★★★★★
()

Here 4 files that doing what you need:

- db.txt - is the database

- set.php - this file puts new lines into database

- get.php - this script shows in 2 different ways messages

- del.php - clears database

Listing set.php

<?php

// full path to db file
define("DB_FILE", "db.txt");

echo $_GET["line"];

$f=fopen(DB_FILE, "a");
fwrite($f, $_GET["line"]."--".time()."\n");

echo "<br>done!";

?>

This works like this: you send a GET-request like this: site.com/chat/set.php?line=I-was-looking-for-you-for-you-for-you and your message is writing into database file db.txt with a timestamp in the end

Listing db.txt

I-was-looking-for-you-for-you-for-you--1537393917

Listing get.php

<?php

// full path to db file
define("DB_FILE", "db.txt");
 
 
function read_tail($file, $lines) {
    //global $fsize;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}
 

// main start
if (isset($_GET["mode"]) && $_GET["mode"]=="full") {
	$f=file_get_contents(DB_FILE);
	echo "<pre>".$f."</pre>";

} elseif (isset($_GET["n"]) && is_numeric($_GET["n"])) {
	$fsize = round(filesize(DB_FILE)/1024,2);
	echo "File size is {$fsize} Kb\n\n<br>";
	echo "Last ".$_GET["n"]." lines of the file:\n\n<br><br>";
	echo "<pre>\n";
	$lines = read_tail(DB_FILE, $_GET["n"]);
	foreach ($lines as $line) {
		echo $line;
	}
	echo "</pre>\n";

} else {
	//readl last line
	$f=fopen(DB_FILE, "r");
	$cursor = -1;
	fseek($f, $cursor, SEEK_END);
	$char = fgetc($f);
	// Trim trailing newline chars of the file
	while ($char === "\n" || $char === "\r") {
		fseek($f, $cursor--, SEEK_END);
		$char = fgetc($f);
	}
	// Read until the start of file or first newline char
	while ($char !== false && $char !== "\n" && $char !== "\r") {
		//Prepend the new char
		$line = $char . $line;
		fseek($f, $cursor--, SEEK_END);
		$char = fgetc($f);
	}
	
	echo $line;
}

echo "<br>done!";

?>

Here are, as you can see, two methods of viewing data: site.com/chat/get.php?mode=full - this displays all the lines. and site.com/chat/get.php?n=1 or n=2 - it shows last n lines.

Listing del.php

<?php


// full path to db file
define("DB_FILE", "db.txt");

file_put_contents(DB_FILE, "");

echo "<br>clear!";

?>

site.com/chat/del.php - deletes all data.

So the password and other options are in your further work.

znavko ★★
()
Последнее исправление: znavko (всего исправлений: 1)

Вот часть по получению файла правда тут я рандомлю исходное множество для казино, но если строку random1= заменить на message= то будет получение сообщения с сайта

void__ Math::SetRotate()
{
	if(!Drum_->GetAllRoll()&&GetCredits()>=GetTotalBet()&&GetWin_()==0)
    {
#if netsupport==1
        /*
		Con_->SetBuf("Give Me Next Datagramm",0);
        Con_->Snd(0);
		Con_->ClearBuf(0);
        Con_->Recv(0);
        Drum_->SetBuffer(Con_->GetBuf(0));
        Con_->ClearBuf(0);
		*/
		const std::string header_filename = "head.txt";
		const std::string body_filename = "body.html";
#ifdef _WINDOWS_2
		FILE *header_file;
		fopen_s(&header_file, header_filename.c_str(), "w");
#else
		FILE *header_file = fopen(header_filename.c_str(), "w");
#endif
		//if (header_file == NULL)
		//	return -1;
#ifdef _WINDOWS_2
		FILE *body_file;
		fopen_s(&body_file, body_filename.c_str(), "w");
#else
		FILE *body_file = fopen(body_filename.c_str(), "w");
#endif
		//if (body_file == NULL)
		//	return -1;
		CURL *curl;
		CURLcode res;
		curl = curl_easy_init();
		if (curl) {
			curl_easy_setopt(curl, CURLOPT_URL, "http://getlucky2.ucoz.net");
			// example.com is redirected, so we tell libcurl to follow redirection 
			curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
			curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0");
			curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); 
			curl_easy_setopt(curl, CURLOPT_WRITEDATA, body_file);
			curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
			// сохраняем заголовок
			curl_easy_setopt(curl, CURLOPT_WRITEHEADER, header_file);
			//USING CURLOPT NOBODY
			//curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
			// Perform the request, res will get the return code 
			res = curl_easy_perform(curl);
			// Check for errors 
			//if (res != CURLE_OK)
			//	std::cout << "curl_easy_perform() failed: %s\n" << curl_easy_strerror(res) << std::endl;
			// always cleanup 
			curl_easy_cleanup(curl);
		}
		fclose(body_file);
		fclose(header_file);
#ifdef _WINDOWS_2
		fopen_s(&body_file, body_filename.c_str(), "r");
#else
		body_file = fopen(body_filename.c_str(), "r");
#endif
		//if (body_file == NULL)
		//	return -1;
		char *str = new char[64];
		std::string stroke = "";
		while (!feof(body_file))
		{
			fgets(str, 64, body_file);
			if (strstr(str, "random1="))
				stroke += str;
		}
		fclose(body_file);
		delete[] str;
		int j = 0;
		while (stroke[j++] != '=')
			;
		std::string *strmas = new std::string[14];
		for (int i = 0; i < 14; i++)
		{
			while (stroke[j] != '|')
				strmas[i] += stroke[j++];
			strmas[i] += stroke[j++];
			//std::cout << strmas[i] << std::endl;
		}
		std::string totalstr = "";
		srand(time(0));
		int max_ = 14, min_ = 0;
		for (int i = 0; i<30; i++)
			totalstr += strmas[((int__)((float__)rand() / RAND_MAX*(max_ - min_) + min_))];
		//std::cout << totalstr << std::endl;
		Drum_->SetBuffer((char__*)totalstr.c_str()/*"4|5|6|7|8|9|10|11|12|13|14|15|16|17|4|5|6|7|8|9|10|11|12|13|14|15|16|17|4|5"*/);
#endif
        Drum_->Start();
        for(int__ i=0;i<GetCountDrums();i++)
        {
            if(!i)
                fps = 0;
            time_[i]=clock();
            Drum_->Set(1+Drum_->Get(i),i);
            StartAcceleration(i);
            Drum_->SetRoll(true,i);
        }
    }
}


чуствую себя как на хабре мини статейки такие
Gremlin_
() автор топика
Ответ на: комментарий от Gremlin_
size_t write_data(char *ptr, size_t size, size_t nmemb, FILE* data)
{
	return fwrite(ptr, size, nmemb, data);
}
Gremlin_
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.