[HELP]HTTP_POST Request
#1

Hey Guys
I'm having a problem with the HTTP() function.
I wanted to send some data in a file in another domain via a HTTP_POST request, but I'didnt managed to do it due to a HTTP_ERROR_MALFORMED_RESPONSE (6) response code.

This is what i made in Pawn

Код:
#include <a_http>
#include <a_samp>

forward Response(index,response_code,data[]);

public OnFilterScriptInit()
{
	HTTP(1,HTTP_POST,"mydomain.altervista.org/GenericFile.txt","something","Response");
	return 1;
}

public Response(index,response_code,data[])
{
	printf("response %d",response_code);
	return 1;
}
The file is currently empty.

What should I do?

Thank you very much
Reply
#2

Well... since you use HTTP_POST, give a try to this:

PHP код:
new v,
    
data[50];
format(data50"?value=%d"v);
HTTP(playeridHTTP_POST"localhost/myfile.php"data"MyResponse"); 
Then use $_POST into the .php file, I don't know if this works because I've never used HTTP and I can't test right now.

Good luck.
Reply
#3

Ok Thank you, now it gave me the response code 200, but it didn't write anything in my txt file.
What I want to do is to write a string into that file, in case i didn't explain my self.
Reply
#4

Use the .php file to write, I think you can't write throught HTTP, it's used just to send/request data.
Reply
#5

Ah ok, I've figured out and learnt something about php but not to much. Someone can give me an example of php page that receives my http_post request?
What i really want to do is a command (/bug [string]) that send a string into a txt file via http request and this php page.
Reply
#6

PHP код:
<?php
if(isset($_POST['bug'])) {
    
// Create fancy date string
    
$dateTimeStr "[ ".date('l \t\h\e jS \o\f F \a\t H:s')." ]";
    
    
// Open txt file for writing
    
$fh fopen("path/to/textfile.txt","a+");
    
    
// Write
    
fwrite($fh,$dateTimeStr.PHP_EOL.$_POST['bug'].PHP_EOL.PHP_EOL);
    
    
// Close the file (for memory preservance)
    
fclose($fh);
    
    
// Profit!
}
?>
Reply
#7

Ok Thank you but now I have again the response code 6, maybe it's wrong the way i formatted the request in pawn?
The code is almost the same
Код:
HTTP(1,HTTP_POST,"mydomain.altervista.org/GenericFile.php","something","Response");
Reply
#8

Yea, your "something" is all wrong, try something like the below (just using name and vehicleid as an example, change this to handle bug text instead, the key here is the myPostString bit and the format function call)

Код:
new myPostString[128],
       name[32],
       vehicleId;

GetPlayerName(playerid, name, 32);
vehicleId = GetPlayerVehicleID(playerid);

format(myPostString, sizeof(myPostString), "name=%s&vehicle_id=%s", name, vehicleId);

HTTP(1, HTTP_POST, "mydomain.altervista.org/GenericFile.php", myPostString, "Response");
Reply
#9

Ok guys, Thank you very much for your time and help, it give me again the response code 6 but, you know what? It works! I really appreciate your help and now I learnt something new.
Reply
#10

My pleasure, +rep's are always appreciated :P (the little star icon at bottom of left column of posts)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)