cURL回应400

时间:2013-04-08 02:12:13

标签: php xml http curl

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

$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

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

2 个答案:

答案 0 :(得分: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>

网址无效,包含特殊字符,例如空格字符。

您需要正确构建它,PHP可以通过http_build_query()函数为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);

这应该可以解决您对请求网址的问题。请求仍然可能存在更多问题/其他错误。当您使用curl时,您可能会找到 Php - Debugging Curl Bad Request. Connecting to sites via curl on host and system <中列出的信息/ strong>以及。

答案 1 :(得分:0)

不禁注意到您所联系的网址中的XML代码。

你确定应该在那里吗?

如果是,您可能需要对其进行正确的URL编码。代码400表示您尝试访问的URL不存在。由于返回的网址与您发送的网址不匹配,因此该指向为真。