PHP发布到ASP表单,将XML结果返回到PHP页面。

时间:2022-11-06 01:57:08

The setup of what i have to work with and what i need is as below:

我必须使用的设置和我需要的设置如下:

  • I have been provided with an ASP form (on another domain) which upon submission outputs search results in an XML format.

    我已经提供了一个ASP表单(在另一个域上),在提交时以XML格式输出搜索结果。

  • I am developing a PHP website (on my companies domain)

    我正在开发一个PHP网站(在我的公司域名上)

  • From this PHP website i need to be able to query the said ASP form and get the XML results posted back to the PHP page (on my companies domain)

    从这个PHP网站我需要能够查询所述ASP表单并将XML结果发回PHP页面(在我的公司域上)

  • The variable "Client=*" must be sent to the ASP form, for it to work.

    变量“Client = *”必须发送到ASP表单才能生效。

What i have tried so far...

到目前为止我尝试过的......

  • jQuery.ajax to try and do a normal post request using this code:

    jQuery.ajax尝试使用此代码执行正常的发布请求:

    $.ajax({
    url: "http://www.example.com/xml/aspfile.asp",
    crossDomain: true,
    cache: false,
    dataType: ($.browser.msie) ? "text" : "xml",
    data: { Client: "clientname" etc },
    type: 'post',
    xhrFields: {
        withCredentials: true
    },
    error: function(){
        alert("error");
    },
    success: function(data){
        var xml;
        if (typeof data == "string") {
            xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            xml.loadXML(data);
        } else {
            xml = data;
        }
    } });
    

Note:

注意:

I have tried alot of different iterations of the above code from basic ajax requests to more complex ones like the above.

我已经尝试了很多不同的上述代码迭代,从基本的ajax请求到更复杂的代码,如上所述。

The above code returns the following error, which is an error i have come across quite a bit.

上面的代码返回以下错误,这是一个错误,我遇到了很多。

XML Parsing Error: no element found Location: moz-nullprincipal:{14ce834e-ef24-43f8-b338-7202241298a5} Line Number 1, Column 1:^

What i need

我需要的

Ideally some code that works ... failing that ideas or suggestions on how i can get this to work.

理想情况下,一些代码可以工作......没有关于如何让它工作的想法或建议。

Many thanks in advance to all those that post answers for your time.

非常感谢所有那些为你的时间发布答案的人。

Edit: As requested here is how the XML looks

编辑:这里要求的是XML的外观

<quicksearchresults>
        <heading>
            <title1>River Cruises</title1>
            <title2>Quick Search</title2>
            <cruise_nos>732</cruise_nos>
            <earliest_date>01/08/11</earliest_date>
            <latest_date>01/09/11</latest_date>
            <river>Anywhere</river>
            <linename>Any</linename>
        </heading>
        <rivercruiselist>
            <holdate>28/08/11</holdate>
            <linename>The River Cruise Line</linename>
            <shipname>Esmerelda</shipname>
            <shiplink>url</shiplink>
            <cruisename>Cruise+The+Danube+to+Vienna+%26+Budapest</cruisename>
            <cruiselink>url</cruiselink>
            <river>Danube</river>
            <ratingicon>Images/BudgetIcon.png</ratingicon>
            <flyfrom>Linz</flyfrom>
            <flyback>linz</flyback>
            <cruisenights>7</cruisenights>
            <vacationdays>10</vacationdays>
            <lowprice>0</lowprice>
            <highprice>0</highprice>
            <flights>including Coach</flights>
            <soldout>Yes</soldout>
            <enquiryformlink>url</enquiryformlink>
            <enquiryformimage>Images/TravelEButton.png</enquiryformimage>
        </rivercruiselist>
</quicksearchresults>

1 个解决方案

#1


1  

Use your PHP server as a proxy: you make a AJAX request to your own PHP page which uses curl to get the XML from the external source and returns it you, so you can parse it.

使用您的PHP服务器作为代理:您向自己的PHP页面发出AJAX请求,该页面使用curl从外部源获取XML并将其返回给您,以便您可以解析它。

var xml;
if (typeof data == "string") {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = false;
    xml.loadXML(data);
} else {
    xml = data;
}

This part is not necessary, you can use jQuery selectors to parse the XML. From the looks of it your request isn't returning any results. What does alert(xml) produce? AFAIK you cannot do a cross-domain POST, you can do cross-domain GET with some JSONP hacks though.

这部分不是必需的,您可以使用jQuery选择器来解析XML。从它的外观来看,您的请求不会返回任何结果。 alert(xml)产生什么? AFAIK你不能做跨域POST,你可以用一些JSONP黑客做跨域GET。

Your example XML does not appear to be valid as the first <quicksearchresults> node is never closed.

您的示例XML似乎无效,因为第一个 节点永远不会关闭。

Here's an example, you'll have to excuse my broken PHP, as I haven't used it in a while.

这是一个例子,你将不得不原谅我破碎的PHP,因为我有一段时间没有使用它。

// post the contents of the form to our PHP proxy

    $.post("myproxy.php", $("#myform").serialize, function(data) {
    // do something with returned xml here.

    },"xml");

Proxy example (myproxy.php):

代理示例(myproxy.php):

 <?php 

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://www.remotedomain.com/api.php");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'client*' => 'something',
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        echo $result;
 ?>

#1


1  

Use your PHP server as a proxy: you make a AJAX request to your own PHP page which uses curl to get the XML from the external source and returns it you, so you can parse it.

使用您的PHP服务器作为代理:您向自己的PHP页面发出AJAX请求,该页面使用curl从外部源获取XML并将其返回给您,以便您可以解析它。

var xml;
if (typeof data == "string") {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = false;
    xml.loadXML(data);
} else {
    xml = data;
}

This part is not necessary, you can use jQuery selectors to parse the XML. From the looks of it your request isn't returning any results. What does alert(xml) produce? AFAIK you cannot do a cross-domain POST, you can do cross-domain GET with some JSONP hacks though.

这部分不是必需的,您可以使用jQuery选择器来解析XML。从它的外观来看,您的请求不会返回任何结果。 alert(xml)产生什么? AFAIK你不能做跨域POST,你可以用一些JSONP黑客做跨域GET。

Your example XML does not appear to be valid as the first <quicksearchresults> node is never closed.

您的示例XML似乎无效,因为第一个 节点永远不会关闭。

Here's an example, you'll have to excuse my broken PHP, as I haven't used it in a while.

这是一个例子,你将不得不原谅我破碎的PHP,因为我有一段时间没有使用它。

// post the contents of the form to our PHP proxy

    $.post("myproxy.php", $("#myform").serialize, function(data) {
    // do something with returned xml here.

    },"xml");

Proxy example (myproxy.php):

代理示例(myproxy.php):

 <?php 

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://www.remotedomain.com/api.php");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'client*' => 'something',
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        echo $result;
 ?>