开发者

PHP Post to ASP Form, return XML results to PHP page.

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.

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

  • 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)

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

What i have tried so far...

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

    $.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.

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

XML Parsing Error:开发者_如何学Go 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

<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>


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.

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.

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

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

// 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):

 <?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;
 ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜