-3

i got a problem in PHP. i want to get all the bus stations in india. so i have made an xml request and post that xml data to given API URL. but i did not get the result. below is the .net code which is working fine. even i have used the "HttpRequest" method in php, but it gives me an error like "Fatal error: Class 'HttpRequest' not found".

can anybody help me to give me the equivalent code in php or possible ways to get the data..?

thanks in advance.

.net code (working fine)

protected void getBusServices(string journeydate)
{

        XmlDocument doc = new XmlDocument();
        StringReader stream;
        StreamReader reader;
        XmlTextReader textreader;
        HttpWebRequest req;
        HttpWebResponse response;
        try
        {

            string password = "password";
            DateTime dt = Convert.ToDateTime(journeydate);
            string strJrneyDate = dt.ToString("MM-dd-yyyy");
            string strRtDate = dt.AddDays(3).ToString("MM-dd-yyyy");
            string url = Bus_Api_Url;


             string RequestCommand = "<Command>GET_STATIONS</Command> <Username>username</Username> <Password>password</Password>";
            req = (HttpWebRequest)WebRequest.Create(url);
            string RequestData = "RequestXML=<?xml version='1.0' encoding='utf-8' ?><BusRequest  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + RequestCommand + "</BusRequest>";
            req.Method = WebRequestMethods.Http.Post;
            req.ContentLength = RequestData.Length;
            req.ContentType = "application/x-www-form-urlencoded";
            using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.Write(RequestData);
            }
            response = (HttpWebResponse)req.GetResponse();
            Stream responsestream = response.GetResponseStream();
            reader = new StreamReader(responsestream);
            string xmlresponse = reader.ReadToEnd();
            stream = new StringReader(xmlresponse);
            textreader = new XmlTextReader(stream);
            doc.LoadXml(xmlresponse);
            DataSet ds = new DataSet();
            ds.ReadXml(textreader);


        }

        catch (Exception ex)
        {
            EBUtils.Logger.LogError.Publish(ex);
        }
        finally
        {
            reader = null;
            // stream = null;
            reader = null;
            response = null;
            doc = null;
        }
    }

}
4

1 回答 1

0

它非常简单,请查看 http://php.net/manual/en/simplexml.examples-basic.php

我已经多次使用相同的方法,这是我的一个活动 php 项目中的一个小片段。

$file = "http://the_address_of_the_xml_file.com";
if(!$xml = simplexml_load_file($file)) {
return 0;
} else {     //open
$status = $xml->attributes()->statu
if($status=='ok') {//open
$bio = $xml->artist->bio;


//closed
for ($i = 0; $i < 1; $i++) {

file_get_content 怎么样?

$post_data = array('xml' => $xml_str);
$stream_options = array(
    'http' => array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
        'content' =>  http_build_query($post_data)));

$context  = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);

哈哈,我今天状态不佳,在床上真的不舒服;)

于 2013-02-09T07:00:09.613 回答