开发者

How to download XML content generated by PHP script using WebClient?

开发者 https://www.devze.com 2023-01-14 05:32 出处:网络
I have a PHP script which generates a XML using \'echo\' commands and data from a database. So, if I access this script from a browser, I can see the xml data and download it.

I have a PHP script which generates a XML using 'echo' commands and data from a database. So, if I access this script from a browser, I can see the xml data and download it.

So I writting a software to retrieve this data using webclient class. But webclient only downloads a empty file, so I'm thinking it's trying to download the .php file, and not the dynamic 开发者_StackOverflowcontent generated.

The PHP script is sending a header("Content-type: text/xml") and the webclient tries to download from https://mySecureServer.com/db/getXMLData.php (Maybe that's the problem).

Any ideas?

EDIT: WebClient Code (just ripped some local file operations):

string url = @"https://mySecureServer.com/db/getXMLData.php";
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompletedEvtHdl);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChangedEvtHdl);
client.BaseAddress = @"https://mySecureServer.com/db/";
client.DownloadFileAsync(new Uri(url), toSavePath + filename);


If you just want to download the XML data which this getXMLData.php script is generating, then you can directly use cURL to get its contents:

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_SSL_VERIFYPEER => false     // disable certificate checking
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

//Now get the webpage
$data = get_web_page( "https://mySecureServer.com/db/getXMLData.php" );

//Display the data (optional)
echo "<pre>" . $data['content'] . "</pre>";
0

精彩评论

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