dongyan7950 2014-12-18 22:40
浏览 33

一旦actionscript将数据发送到它,php应该如何反应?

I have decent PHP and AS3 knowledge, but none regarding connecting the two. I am trying to use AS3 to send data to PHP, and have PHP save them to a XML.

What I don't get is how php is supposed to react to external code. The AS3 script does not start the page, correct? It just sends data to it? Just executing the actionscript side doesn't work nor does loading the php in the browser manually (with apache running correctly, gibberish echo's funcioning) or reloading it after the AS3 sending. The xml stays empty, no return value to trace. All tutorials deal with before and after, assuming that I know how it is that php will react at all to receiving data from AS3. Can someone tell me my missing link?

If it matters, my relevant AS3:

        public function xt(e:MouseEvent):void
    {
        marque = tmarque.text;
        prix = tprix.text;
        couleur = tcouleur.text;
        xmlString = "<?xml version='1.0' encoding='utf-8'?>";
        fichier = new XML(xmlString);
        fichier = <ordinateurs />;
        fichier.marque = marque;
        fichier.couleur = couleur;
        fichier.PRIX = prix;
        var req = new URLRequest("C:/xampplite/htdocs/save_xml.php");
        //(have also tried placing the php in same folder)

        req.data = fichier;
        req.contentType = "text/xml";
        req.method = URLRequestMethod.POST;
        var xmlSendLoad:URLLoader = new URLLoader();
        xmlSendLoad.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
        xmlSendLoad.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
        xmlSendLoad.load(req);
}

function onComplete(event:Event):void {
try {
    var xmlResponse = new XML(event.target.data);
    TXTtesticle.text = xmlResponse;
    removeEventListener(Event.COMPLETE, onComplete);
    removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
} catch (err:TypeError) {
    TXTtesticle.text = "An error occured when communicating with server:
" + err.message;
}
trace (xmlResponse);
placeText();
}

and PHP

<?php 
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{  
    $xml = $GLOBALS["HTTP_RAW_POST_DATA"]; 
    $file = fopen("data.xml","wb"); 
    fwrite($file, $xml); 
    fclose($file); 
    echo($GLOBALS["HTTP_RAW_POST_DATA"]); 
} 
?>
  • 写回答

1条回答 默认 最新

  • dongmian8108 2014-12-18 22:52
    关注

    Using AS to call PHP is basically exactly the same thing as Ajax. Just rather than an Ajax call from client JS in a browser, its from AS in Flash.

    Two issues I see instantly with your code:

    (1) $GLOBALS["HTTP_RAW_POST_DATA"]? Why? That's deprecated as of PHP 5.6.0. And it was bad practice long before that. There's this thing called $_POST that everyone else has been using for a long time now.

    (2) var req = new URLRequest("C:/xampplite/htdocs/save_xml.php"); You don't do URL requests to the local file system. You do URL requests to a web url, like var req = new URLRequest("http://localhost/save_xml.php");

    To understand what your PHP should do with the POST, you can look at any Ajax tutorial. It need not be AS specific because a client making a http request to a webserver is a client making a http request to a webserver no matter what the client is.

    评论

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效