1

Hello to anyone willing to help.. I don't know what else to do. I am trying to send an xml request to an api which will give back an xml formatted response. I am using curl to achieve it. But no matter what way I format my request, I always get a 400 response. The url is formatted correctly. Here is my code:

$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');

Here is the info I get back from 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

What am I doing wrong? Any help would be appreciated.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
lov2code
  • 394
  • 1
  • 6
  • 19
  • I'm not supper familiar with curl however from my exeprience using the command `curl -O http://url_to_your_api_call` should work this will get the xml, which you can then choose to handle however you choose – brendosthoughts Apr 08 '13 at 02:19
  • Thanks for the reply, but I do not understand where I am supposed to put that. Could you take it from my code to better understand? Thanks – lov2code Apr 08 '13 at 02:31
  • I can try to help first have you tried taking a look in poi.xml in the same working directory where the script is called from ? as from what curl is telling you a 400 responce is http code meaning you have a bad request (non-properly formated) this formating is specific to your api (something i have no knowledge of) – brendosthoughts Apr 08 '13 at 02:49
  • There are more: Possible duplicate of [400 - Bad Request in Curl](http://stackoverflow.com/q/13764623/367456); Possible duplicate of [Problem fetching XML data using Expedia API and curl](http://stackoverflow.com/q/6703004/367456); Possible alternative approach with less code: [Using SimpleXML to load remote URL](http://stackoverflow.com/q/8693043/367456) – hakre Apr 08 '13 at 10:26

2 Answers2

2

The problem you face has less to do with curl but more with the URL you are using:

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>

The URL is invalid, it contains special characters, for example the space character.

You need to build it properly, PHP can help you with that by using some variables and an array for the URLs query parameters via the http_build_query() function that encodes everything properly for you:

// 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>',
));

All you need to do then is to pass it into curl:

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

# or just:

$ch1 = curl_init($url);

This should fix the issues you have with the request URL. There still might more problems / other mistakes with the request. As you're using curl, you might find the information outlined in Php - Debugging Curl or Bad Request. Connecting to sites via curl on host and system as well.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

Can't help but notice the XML code in the URL you are contacting.

Are you sure that is supposed to be there?

If it is, you might need to properly URL encode it. A code 400 means the URL you are trying to reach does not exist. Which would point to being true since the returned url does not match your sent url.

francisco.preller
  • 6,559
  • 4
  • 28
  • 39