1

您好,任何愿意帮助的人.. 我不知道还能做什么。我正在尝试向 api 发送一个 xml 请求,它将返回一个 xml 格式的响应。我正在使用 curl 来实现它。但无论我以何种方式格式化我的请求,我总是得到 400 响应。url 格式正确。这是我的代码:

$ch1 = curl_init(); 
    $fp1 = fopen('poi.xml','w'); 
    curl_setopt($ch1, CURLOPT_URL, "http://api.ean.com/ean-services/rs/hotel/v3/geoSearch?cid=55505&minorRev=14&apiKey=xkr94zxbsjnzczryqajmjg3g&locale=en_US&currencyCode=USD&customerIpAddress=189.214.86.135&customerUserAgent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31&xml=<LocationInfoRequest><destinationString>Dallas, TX</destinationString><type>2</type></LocationInfoRequest>");  
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
    curl_setopt($ch1, CURLOPT_HEADER, 0); 
    curl_setopt($ch1, CURLOPT_FILE, $fp1);
    curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'); 
    $val1 = curl_exec($ch1);
    $info = curl_getinfo($ch1);
    echo '<pre>';
      print_r($info);
      echo '</pre>';
    curl_close($ch1);//Close curl session 
    fclose($fp1); //Close file overwrite 
    $avail = simplexml_load_file('poi.xml');

这是我从 curl 获得的信息:

[url] => http://api.ean.com/ean-services/rs/hotel/v3/geoSearch?cid=55505&minorRev=14&apiKey=xkr94zxbsjnzczryqajmjg3g&locale=en_US¤cyCode=USD&customerIpAddress=189.214.86.135&customerUserAgent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31&xml=Dallas, TX2
[content_type] => text/html
[http_code] => 400
[header_size] => 158
[request_size] => 558
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.075402
[namelookup_time] => 0.038232
[connect_time] => 0.055687
[pretransfer_time] => 0.055787
[size_upload] => 0
[size_download] => 349
[speed_download] => 4628
[speed_upload] => 0
[download_content_length] => 349
[upload_content_length] => 0
[starttransfer_time] => 0.07531
[redirect_time] => 0

我究竟做错了什么?任何帮助,将不胜感激。

4

2 回答 2

2

您面临的问题与 curl 关系不大,而与您使用的 URL 关系更大:

http://api.ean.com/ean-services/rs/hotel/v3/geoSearch?cid=55505&minorRev=14&apiKey=xkr94zxbsjnzczryqajmjg3g&locale=en_US&currencyCode=USD&customerIpAddress=189.214.86.135&customerUserAgent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31&xml=<LocationInfoRequest><destinationString>Dallas, TX</destinationString><type>2</type></LocationInfoRequest>

URL 无效,它包含特殊字符,例如空格字符。

http_build_query()您需要正确构建它,PHP 可以通过为您正确编码所有内容的函数为 URL 查询参数使用一些变量和数组来帮助您:

// create api.ean.com request URL:
$url = 'http://api.ean.com/ean-services/rs/hotel/v3/geoSearch?' . http_build_query(array(
    'cid'               => '55505',
    'minorRev'          => '14',
    'apiKey'            => 'xkr94zxbsjnzczryqajmjg3g',
    'locale'            => 'en_US',
    'currencyCode'      => 'USD',
    'customerIpAddress' => '189.214.86.135',
    'customerUserAgent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31',
    'xml'               => '<LocationInfoRequest><destinationString>Dallas, TX</destinationString><type>2</type></LocationInfoRequest>',
));

然后你需要做的就是将它传递给 curl:

$ch1 = curl_init();
...
curl_setopt($ch1, $url);  

# or just:

$ch1 = curl_init($url);

这应该可以解决您对请求 URL 的问题。请求可能还有更多问题/其他错误。当您使用 curl 时,您可能会发现Php - 调试 Curl错误请求中列出的信息。也通过主机和系统上的 curl 连接到站点

于 2013-04-08T10:42:12.647 回答
0

不禁注意到您正在联系的 URL 中的 XML 代码。

你确定那应该在那里吗?

如果是,您可能需要对其进行正确的 URL 编码。代码 400 表示您尝试访问的 URL 不存在。由于返回的 url 与您发送的 url 不匹配,这将表明为真。

于 2013-04-08T03:04:14.317 回答