Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

need equivalent PHP (HttpWebRequest and Stream or curl) code for this .net code

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

its really simple, check out http://php.net/manual/en/simplexml.examples-basic.php

i've used the same method lot of times, heres a little snippet from one of my active php projects.

$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++) {

and how about file_get_content ?

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

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

ha im not on top form today, in bed really unwell ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...