PHP Curl

时间:2015-08-03 14:53:03

标签: php curl

我在下面的代码中遇到了CURL的问题。超链接我在底部生成如果点击是从API生成正确的输出(在浏览器中显示XML文件)

然而,当通过CURL发送时,没有实现响应。知道我在这里失踪了吗?在更多错误检查方面我能做些什么吗?

(明确删除了应用程序密钥)

<?

$APIKey = '123456';
$maxResponseTime = '5000';
$maxResultCount = '5';
$Brand = 'OTB';
$DepartureAirport = 'LON';
$DestinationResort = 'Magaluf';
$DepartureDate = '2015-09-08';
$HolidayMinNights = '7';
$HolidayMaxNights = '7';
$DaysEitherSide = '3';
$PassengerCounts_Adults = '2';







$QueryString = "APIKey=".$APIKey."&maxResponseTime=".$maxResponseTime."&maxResultCount=".$maxResultCount."&Brand=".$Brand."&DepartureAirport=".$DepartureAirport."&DestinationCountry=&DestinationRegion=&DestinationResort=".$DestinationResort."&DepartureDate=".$DepartureDate."&HolidayMinNights=".$HolidayMinNights."&HolidayMaxNights=".$HolidayMaxNights."&Board=&StarRatingMin=&StarRatingMax=&DaysEitherSide=".$DaysEitherSide."&PassengerCounts_Adults=".$PassengerCounts_Adults."&PassengerCounts_Children=0&PassengerCounts_ChildAge=&PassengerCounts_Infants=0 ";
$QueryString2 = 'http://v2.feeds.distributenetwork.net/singlebrand.asmx/GetDeals?'.$QueryString;

// Get cURL resource
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 ); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ($curl, CURLOPT_URL, $QueryString2);

// Send the request & save response to $resp
$resp = curl_exec($curl);

if(!curl_exec($curl)){
 echo('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
// Close request to clear up some resources
curl_close($curl);

echo $resp;


///Generate Manual Link To Complete Get Request In Browser
echo '<br><br><a href='.$QueryString2.'>A Manual Link Which Works When Clicked</a>';


?>

2 个答案:

答案 0 :(得分:1)

您的问题出现在$QueryString末尾的空白处 删除它,它将工作=)

答案 1 :(得分:1)

你有空格问题,请使用http_build_query

$queryArr = array(
    "APIKey"                 => '123456',
    "maxResponseTime"        => '5000',
    "maxResultCount"         => '5',
    "Brand"                  => 'OTB',
    "DepartureAirport"       => 'LON',
    "DestinationResort"      => 'Magaluf',
    "DepartureDate"          => '2015-09-08',
    "HolidayMinNights"       => '7',
    "HolidayMaxNights"       => '7',
    "DaysEitherSide"         => '3',
    "PassengerCounts_Adults" => '2',
    "DestinationCountry"     => '',
    "DestinationRegion"      => '',
    "DestinationResort"      => '',
    "board"                  => '',
    "StarRatingMin"          => '',
    "StarRatingMax"          => '',
    "PassengerCounts_Children" => '',
    "PassengerCounts_ChildAge" => '',
    "PassengerCounts_Infants"  => ''

);

define("BASE_URL",'http://v2.feeds.distributenetwork.net/singlebrand.asmx/GetDeals?');

// Get cURL resource
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 ); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ($curl, CURLOPT_URL, BASE_URL.http_build_query($queryArr));

// Send the request & save response to $resp
$resp = curl_exec($curl);