【问题标题】:Issue in getting data using CURL使用 CURL 获取数据的问题
【发布时间】:2013-10-08 16:59:21
【问题描述】:

我正在使用 CURL 向 API 发送请求,但无法获取请求。当您直接在浏览器中调用 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 代码。请指教我做错了什么。

【问题讨论】:

  • 403 禁止:您的凭据无效。就这么简单。
  • $post_string1 中删除网址,它应该只包含帖子参数。 $post_string1 = 'cid=55505&amp;minorRev=99&amp;apiKey=5d9cp7nfxruc7p788fvvqpwn&amp;locale=en_US&amp;currencyCode=USD&amp;type=xml&amp;hotelId=123912';
  • 抱歉,我修改了我的问题。它的 405 代码。错字:)
  • 405 方法不允许:当服务器期待 GET 时,您不能使用 POST(反之亦然)
  • 是的,但我们想知道为什么 shazad 的代码不起作用而您的代码起作用。我们不仅在寻找代码,而且在寻找知识!

标签: php xml curl


【解决方案1】:

检查一下

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

【讨论】:

  • 谢谢斯里维。我认为此 api 中不允许使用 POST 方法。我可以在循环中使用此代码来获取多个酒店的数据吗?
  • 如果您想要多个酒店结果,请使用api.ean.com/ean-services/rs/hotel/v3/list?minorRev=14。请阅读此文档developer.ean.com。它包含您对示例的所有需求
  • 我的意思是,在 foreach 或 for 循环中不断请求不同的酒店 ID。可能吗?
  • @M Shahzad Khan Ya 当然
  • @NathanSrivi 你不应该为了积分而如此努力。在前 24 小时内接受答案是一种不好的做法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 2021-07-15
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 2017-10-25
  • 2021-02-04
相关资源
最近更新 更多