So, I am a total noob in cURL, but since I’m having some problems using php to insert data into a database via flash as3(the files are in diferent servers), I was advised to use a cURL script to bridge them both.
So here is my cURL code (this was copy pasted from another question, I just changed the values, so excuse me if there is any obvious error here):
<?php
//where are we posting to?
$url = 'url'; //I have the correct url of the file (insert.php) on the other server
//what post fields?
$fields = array(
'Nome'=>$_POST['nome'],
'Email'=>$_POST['email'],
'Idade'=>$_POST['idade'],
'Profissao'=>$_POST['profissao'],
'Pais'=>$_POST['pais']
);
//build the urlencoded data
$postvars='';
$sep='';
foreach($fields as $key=>$value)
{
$postvars.= $sep.urlencode($key).'='.urlencode($value);
$sep='&';
}
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
and here is the flash as3
function WriteDatabase():void{
var request:URLRequest = new URLRequest ("curl.php file here");
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.nome = ContactForm.nomefield.text;
variables.email = ContactForm.emailfield.text;
variables.idade = ContactForm.idadefield.text;
variables.profissao = ContactForm.proffield.text;
variables.pais = LanguageField.selectedbutton.text;
request.data = variables;
var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);
function onComplete(event):void{
trace("Completed");
}
}
// I am assuming (incorrectly perhaps) that if is called the same way you would call a php file. It also traces "Completed" inside the flash, so it comunicates with it.
I know that it is calling the other file correctly, because it actually creates an entry in the database, but everything is blank. Every field. I also know that the other file works, because when I test the flash file offline it works, just not when its online.
Any help is apreciated.
Don’t build your own query string. cURL can accept a PHP array and do all the work for you:
curl is not smart enough to realize that you’ve already encoded the string into URL format, so it just sees a plain string getting posted, with no ‘name’ value.
as well,
is not how you use that. CURLOPT_POST is a simple boolean value to signify a POST is being done. If you have no fields at all, then suddenly POST is false and you’re using some other method, e.g. GET, which your client app won’t be expecting.