访问API时,cURL无法正常工作

时间:2012-10-29 18:45:46

标签: api curl

当我通过访问此文件并使用cURL访问API时,下面的代码无效,它给出了以下错误:

卷曲错误 结果 HTTP / 1.1 403 Forbidden X-Mashery-Responder:mashery-web2-lax.mashery.com X-Mashery-Error-Code:ERR_403_DEVELOPER_INACTIVE Con​​tent-Type:text / xml Accept-Ranges:bytes Content-Length:31 Server:Mashery Proxy日期:星期一,2012年10月29日18:41:23 GMT连接:保持活力 403开发人员无效

当我通过它生成的链接直接访问它时它正在工作。有人可以告诉我,如果我使用cURL有什么问题吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

/**
 * Initialize the cURL session
 */
$ch = curl_init();

$city = 'Seattle';
$departure = '11/03/2012';
$arrival = '11/08/2012';
$citycode='US';
$apiKey='***************';
$cid='55505';
$locale='en_US';
$currencyCode='USD';
$minorRev='16';
$customerSessionId='0ABAA874-27EB-E913-A4E2-7B0946904C6D';
$customerIpAddress=$_SERVER['REMOTE_ADDR'];
$customerUserAgent=$_SERVER['HTTP_USER_AGENT'];



$customerUserAgent=urlencode($customerUserAgent);

$url='http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev='.$minorRev.'&amp;cid='. $cid .'&amp;apiKey='. $apiKey.'&amp;customerUserAgent='.$customerUserAgent . '&amp;locale='.$locale.'&amp;currencyCode='.$currencyCode.'&_type=xml';


$xml='&lt;HotelListRequest&gt;&lt;city&gt;';
$xml .= $city;
$xml .='&lt;/city&gt;&lt;countryCode>';
$xml .= $citycode;
$xml .='&lt;/countryCode&gt;&lt;arrivalDate&gt;';
$xml .= $arrival;
$xml .='&lt;/arrivalDate&gt;&lt;departureDate&gt;';
$xml .= $departure;
$xml .='&lt;/departureDate&gt;&lt;/HotelListRequest&gt;';


$main = $url .'&xml='. $xml;
echo $main;
 /**
 * Set the URL of the page or file to download.
 */
 curl_setopt($ch, CURLOPT_URL, $main);

  curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 65000);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml" ));
   curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );





 /**
 * Ask cURL to return the contents in a variable instead of simply echoing them to  the browser.
 */
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 /**
 * Execute the cURL session
 */
 $contents = curl_exec ($ch);


 /**
 * Close cURL session
 */
 curl_close ($ch);

 echo '<br /><br />Curl Error';
echo curl_error($ch);
echo '<br />Results <br />' . $contents;


?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在$ url构造中,不要在每个参数之间使用&amp; amp; 编码&符号。应对参数值(例如“xml”参数值)进行编码,但不应对URI中的每个GET请求参数进行分隔的&符号进行编码。

因此,您的代码会创建 $ main 值:

http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=16&amp;cid=55505&amp;apiKey=********&amp;customerUserAgent=&amp;locale=en_US&amp;currencyCode=USD&_type=xml&xml=&lt;HotelListRequest&gt;&lt;city&gt;Seattle&lt;/city&gt;&lt;countryCode>US&lt;/countryCode&gt;&lt;arrivalDate&gt;11/08/2012&lt;/arrivalDate&gt;&lt;departureDate&gt;11/03/2012&lt;/departureDate&gt;&lt;/HotelListRequest&gt;

它应该是:

http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=16&cid=55505&apiKey=********&customerUserAgent=&locale=en_US&currencyCode=USD&_type=xml&xml=&lt;HotelListRequest&gt;&lt;city&gt;Seattle&lt;/city&gt;&lt;countryCode>US&lt;/countryCode&gt;&lt;arrivalDate&gt;11/08/2012&lt;/arrivalDate&gt;&lt;departureDate&gt;11/03/2012&lt;/departureDate&gt;&lt;/HotelListRequest&gt;

保重, Neil @ Mashery