如何使用RESTful POST请求获取xml数据?

时间:2011-07-28 00:40:19

标签: php http rest curl

我无法从healthdata.gov数据服务( http://healthindicators.gov/Developers/ )获取xml。下面,我使用php CURL发出POST请求,我的请求是一个xml字符串。我期待回来找回xml数据。我从服务中得到一个错误,说我没有将内容类型设置为“application / xml”,我做了!

Healthdata.gov是一个庞大而重要的数据源,但奇怪的是他们的文档并不容易(对我来说)理解。有没有人成功从这项服务中提取数据?

提前致谢
(以下代码,应用程序位于此处:http://cd47ddcc.dotcloud.com)。

#index.php

//set URL
$url = 'http://services.healthindicators.gov/v2/REST.svc/Indicators/Filter';

//set xml request
$xml = '
<SearchQuery xmlns="http://schemas.datacontract.org/2004/07/S3.Common.Search" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Mode>And</Mode>
    <Elements>
        <SearchElement i:type="SearchParameter">
            <Name>IndicatorDescriptionID</Name>
            <Operator>Equal</Operator>
            <Value>15</Value>
        </SearchElement>
        <SearchElement i:type="SearchParameter">
            <Name>LocaleID</Name>
            <Operator>Equal</Operator>
            <Value>39</Value>
        </SearchElement>
    </Elements>
    <Page>1</Page>
</SearchQuery>
';

// Get the curl session object
$session = curl_init($url);

// set url to post to 
//curl_setopt($session, CURLOPT_URL,$url);
// Tell curl to use HTTP POST;
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $xml);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml; charset=utf-8"));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// allow redirects 
//curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($session);
//print_r ($response);

echo "<h1>Health Data</h1><br />";
echo $response;
echo "<hr />";

curl_close($session);

0 个答案:

没有答案