当我通过访问此文件并使用cURL访问API时,下面的代码无效,它给出了以下错误:
卷曲错误 结果 HTTP / 1.1 403 Forbidden X-Mashery-Responder:mashery-web2-lax.mashery.com X-Mashery-Error-Code:ERR_403_DEVELOPER_INACTIVE Content-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.'&cid='. $cid .'&apiKey='. $apiKey.'&customerUserAgent='.$customerUserAgent . '&locale='.$locale.'&currencyCode='.$currencyCode.'&_type=xml';
$xml='<HotelListRequest><city>';
$xml .= $city;
$xml .='</city><countryCode>';
$xml .= $citycode;
$xml .='</countryCode><arrivalDate>';
$xml .= $arrival;
$xml .='</arrivalDate><departureDate>';
$xml .= $departure;
$xml .='</departureDate></HotelListRequest>';
$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>
答案 0 :(得分:0)
在$ url构造中,不要在每个参数之间使用&amp; amp; 编码&符号。应对参数值(例如“xml”参数值)进行编码,但不应对URI中的每个GET请求参数进行分隔的&符号进行编码。
因此,您的代码会创建 $ main 值:
http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=16&cid=55505&apiKey=********&customerUserAgent=&locale=en_US&currencyCode=USD&_type=xml&xml=<HotelListRequest><city>Seattle</city><countryCode>US</countryCode><arrivalDate>11/08/2012</arrivalDate><departureDate>11/03/2012</departureDate></HotelListRequest>
它应该是:
http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=16&cid=55505&apiKey=********&customerUserAgent=&locale=en_US¤cyCode=USD&_type=xml&xml=<HotelListRequest><city>Seattle</city><countryCode>US</countryCode><arrivalDate>11/08/2012</arrivalDate><departureDate>11/03/2012</departureDate></HotelListRequest>
保重, Neil @ Mashery