Php 使用Google API和cURL发出批处理请求-不断收到400个错误请求-以下是我的代码:
有点不对劲。我一直收到400个错误请求,但似乎无法收到任何其他错误消息。代码如下:Php 使用Google API和cURL发出批处理请求-不断收到400个错误请求-以下是我的代码:,php,google-app-engine,curl,Php,Google App Engine,Curl,有点不对劲。我一直收到400个错误请求,但似乎无法收到任何其他错误消息。代码如下: $locations = array("LAX", "LGW"); $dateOut = '2015-04-25'; $dateIn = '2015-04-29'; $passengers = 1; $destinations = array("NYC","LAX","CHI","WAS","LAS","SFO","MIA","BOS","DEN","AUS"); $apiQueries = array();
$locations = array("LAX", "LGW");
$dateOut = '2015-04-25';
$dateIn = '2015-04-29';
$passengers = 1;
$destinations = array("NYC","LAX","CHI","WAS","LAS","SFO","MIA","BOS","DEN","AUS");
$apiQueries = array();
foreach ($destinations as $destination) {
foreach ($locations as $origin) {
$data = array("request" => array(
"passengers" => array(
"adultCount" => $passengers
),
"slice" => array(
array(
"origin" => $origin,
"destination" => $destination,
"date" => $dateOut
),
array(
"origin" => $destination,
"destination" => $origin,
"date" => $dateIn
)
),
"solutions" => "10"
));
$apiQueries[] = json_encode($data);
}
}
$batch = '';
$boundary = '--batch_xyzabc';
$i = 0;
foreach ($apiQueries as $data) {
$batch .= "\n" . $boundary . "\nContent-Type: application/http\n\nGET /qpxExpress/v1/trips/search?key=my-unique-key" . " \n" . $data . "\n";
$i ++ ;
if ($i > 48){
// we need to split the batch by packs of <50. Close this one and create the next one
$batch .= "\n" . $boundary . "\n";
$batches[] = $batch;
$batch = '' ;
$i=0;
}
}
// close the current batch pack
if ($i > 0) $batches[] = $batch . "\n" . $boundary . "\n";
for ($i = 0 ; $i < count($batches) ; $i++) {
$session = curl_init('https://www.googleapis.com/batch');
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Host: www.googleapis.com', 'Content-Type: multipart/mixed; boundary=' . $boundary, 'Content-Length: ' . strlen( $batches[$i])));
curl_setopt($session, CURLOPT_POSTFIELDS, $batches[$i]);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, true);
curl_setopt($session, CURLINFO_HEADER_OUT, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($session);
echo $resp;exit; // Shows 400 Bad Request message
$api_response_info = curl_getinfo($session);
$pack_of_answers = substr($resp, $api_response_info['header_size']);
curl_close($session);
}
$locations=数组(“LAX”、“LGW”);
$dateOut='2015-04-25';
$dateIn='2015-04-29';
$乘客=1;
$destinations=数组(“纽约”、“洛杉矶”、“芝加哥”、“华盛顿”、“洛杉矶”、“旧金山”、“迈阿密”、“波士顿”、“丹麦”、“澳大利亚”);
$apiQueries=array();
foreach($destinations作为$destination){
foreach($origin作为$locations){
$data=数组(“请求”=>数组(
“乘客”=>数组(
“成人计数”=>$乘客
),
“切片”=>数组(
排列(
“origin”=>$origin,
“目的地”=>$destination,
“日期”=>$dateOut
),
排列(
“来源”=>$destination,
“目的地”=>$origin,
“日期”=>$dateIn
)
),
“解决方案”=>“10”
));
$apiQueries[]=json_encode($data);
}
}
$batch='';
$boundary='--batch_xyzabc';
$i=0;
foreach($API查询为$data){
$batch.=“\n.$boundary.\n内容类型:application/http\n\nGET/qpxExpress/v1/trips/search?key=我的唯一密钥”。\n.$data.\n”;
$i++;
如果($i>48){
//我们需要将该批按0)$batches[]=$batch.\n“$boundary.\n”的包进行拆分;
对于($i=0;$i
在PHP中调用Google API(无论是否在应用程序引擎上)的最佳方法是使用