-1

我收到对以下代码的 XML 请求的文件提前结束响应。

无法弄清楚错误在哪里。

    $xml = '<?xml version="1.0" ?>';
    $xml .= '<PickUpCityListRQ>';
    $xml .= '<Credentials username="'.$api->username.'" password="'.$api->password.'" remoteIp="'.$api->remoteIp.'" />';
    $xml .= '<Country>UK</Country>';
    $xml .= '</PickUpCityListRQ>';

    $url = 'https://secure.rentalcars.com/service/ServiceRequest.do?serverName=www.rentalcars.com&xml='.utf8_decode(trim($xml));
    $port = 443;

    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $ch = curl_init();                              // initialize curl handle
    curl_setopt($ch, CURLOPT_URL, $url);            // set url to post to
    curl_setopt($ch, CURLOPT_FAILONERROR, true);    // Fail on errors

    if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// allow redirects
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    // return into a variable
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_PORT, $port);          //Set the port number
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);          // times out after 15s
    //curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, '&xml='.$xml);     // add POST fields
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    if ($port==443) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml; charset=UTF-8', 'Accept: application/xml; charset=UTF-8'));
    $data = curl_exec($ch);
    curl_close($ch);

也尝试在帖子字段中发送数据,但没有成功。回复:

<!DOCTYPE DefaultRS SYSTEM 'https://xml.rentalcars.com:443//tj.dtd'><DefaultRS>
<Error id="2">
<Message>Premature end of file.</Message>
</Error>
</DefaultRS>

任何帮助表示赞赏。谢谢

4

2 回答 2

0

您需要先对 xml 数据进行 url 编码,然后才能将其用作查询参数。所以 url 赋值变为:

$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?
  serverName=www.rentalcars.com&xml='.urlencode(utf8_decode(trim($xml)));

请注意,我只是为了便于阅读而将其包装起来——这显然应该放在一行上。

于 2013-06-30T00:16:06.410 回答
0

在尝试了很多变化之后,我终于设法让它工作了。

有两件事引起了问题。

utf8_decode 应该是 utf8_encode

POSTFIELDS 数据应该是一个数组

否则解析器将抛出 Premature end of file 或不发回任何东西。

于 2013-06-30T01:00:27.927 回答