php – 使用CURL获取数据的问题

我正在使用CURL向API发送请求,但无法获取请求.当您直接在浏览器中调用它时,API运行良好.

链接在这里

http://api.ean.com/ean-services/rs/hotel/v3/info?cid=55505&minorRev=99&apiKey=5d9cp7nfxruc7p788fvvqpwn&locale=en_US&currencyCode=USD&hotelId=123912

我的CURL代码在这里

  $post_string1 = "cid=55505&minorRev=99&apiKey=5d9cp7nfxruc7p788fvvqpwn&locale=en_US&currencyCode=USD&type=xml&hotelId=123912";  

  $path1 = "http://api.ean.com/ean-services/rs/hotel/v3/info"; //Relative path to the file with $_POST parsing

$ch1 = curl_init($path1); 
$fp1 = fopen('hotel.xml','w');
curl_setopt($ch1, CURLOPT_POSTFIELDS, $post_string1); //Send the data to the file
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);
$val = curl_exec($ch1);
$info = curl_getinfo($ch1);
curl_close($ch1);//Close curl session
fclose($fp1); //Close file overwrite

//$hotel = simplexml_load_file('hotel.xml');

echo '<pre>';print_r($info); 

我得到了http 405代码.请告诉我我做错了什么.

解决方法:

检查一下

$post_string1 = "http://api.ean.com/ean-services/rs/hotel/v3/info?cid=55505&minorRev=99&apiKey=5d9cp7nfxruc7p788fvvqpwn&locale=en_US&currencyCode=USD&type=xml&hotelId=123912";  
$header[] = "Accept: application/json";
  $header[] = "Accept-Encoding: gzip";
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
  curl_setopt($ch,CURLOPT_ENCODING , "gzip");
  curl_setopt($ch, CURLOPT_CUSTomrEQUEST, 'GET');
  curl_setopt( $ch, CURLOPT_URL, $post_string1 );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  $response = json_decode(curl_exec($ch));
  print_r($response);  exit;

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念