一个旅游网站项目,网站需要机票预订接入了天巡机票接口,获取机票信息,不搞不知道,一搞吓一跳比较麻烦。
搜索机票信息需要分2步,首先POST获得一个SESSION,2秒之后,根据这个SESSION,从一个URL GET 数据 ,并且需要多次GET,这里就用AJXA轮询,
直到返回的GET数据状态为 完成,即可停止轮询。
轮询获取数据之后,还需要自己整理才可显示在网页上,
上PHP代码,,THINKPHP框架代码
1 <?php 2 namespace HomeController; 3 use ThinkController; 4 5 class FlyController extends HomeClassesConfController { 6 public function index(){ 7 $title = $this->title[1]; 8 $keyword = $this->keyword[1]; 9 $desc = $this->desc[1]; 10 $title = str_replace('{lei_name}','机票',$title); 11 $title = str_replace('{site_name}',$this->site_name,$title); 12 $this->assign('title',$title); 13 $this->assign('keyword',$keyword); 14 $this->assign('desc',$desc); 15 $this->display(); 16 } 17 public function piao(){ 18 $post_data = I('post.'); 19 // P($post_data);die; 20 $url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0"; 21 // $url = "http://business.skyscanner.net/apiservices/pricing/v1.0"; 22 $title = $this->title[1]; 23 $keyword = $this->keyword[1]; 24 $desc = $this->desc[1]; 25 $title = str_replace('{lei_name}','机票',$title); 26 $title = str_replace('{site_name}',$this->site_name,$title); 27 $this->assign('title',$title); 28 $this->assign('keyword',$keyword); 29 $this->assign('desc',$desc); 30 $ch = curl_init(); 31 curl_setopt($ch, CURLOPT_URL, $url); 32 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 33 curl_setopt($ch, CURLOPT_POST, 1); 34 curl_setopt($ch, CURLOPT_HEADER, true); 35 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 36 $output = curl_exec($ch); 37 curl_close($ch); 38 //print_r($output); 39 $sessions = between($output,'sg1/v1.0/','Content-Length'); 40 //echo $sessions; 41 $this->assign('sessions',$sessions); 42 $this->assign('apikey',$post_data['apikey']); 43 // echo $sessions. '---'.$post_data['apikey']; 44 $u = U('home/fly/lun',array('b'=>$sessions,'k'=>$post_data['apikey'],'t'=>$post_data['types'],'h'=>$post_data['h'])); 45 exit(json_encode(array('u'=>$u))); 46 //echo $u; 47 $this->assign('u',$u); 48 $this->assign('post_data',$post_data); 49 50 $this->display('index'); 51 } 52 53 public function lun(){ 54 $s = trim($_GET['b']); 55 $a = trim($_GET['k']); 56 $t = trim($_GET['t']); 57 $h = trim($_GET['h']);//h=1表示,第2次,搜索回程 58 $url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/$s?apiKey=$a&mj=".time(); 59 $ch = curl_init(); 60 curl_setopt($ch, CURLOPT_URL, $url); 61 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 62 curl_setopt($ch, CURLOPT_HEADER, false); 63 $output = curl_exec($ch); 64 curl_close($ch); 65 ///print_r($output); 66 file_put_contents('mp_'.$t.'.txt',$output); 67 $j_array = json_decode($output,true); 68 //往返 69 if ($t == 2) 70 { 71 $j = $this->_outhui($j_array) ; 72 }else{ 73 $j = $this->_out($j_array) ; 74 } 75 76 77 78 $jie['Status'] = $j_array['Status']; 79 80 81 $total_price_ci = $j['total_price_ci']; 82 83 unset($j['total_price_ci']); 84 //P($j); die; 85 if (count($j_array['Itineraries']) < 2) 86 { 87 $jie['g'] = 0; 88 } 89 90 $this->assign('h',$h); 91 $this->assign('piao',$j); 92 $this->assign('total_price_ci',$total_price_ci); 93 94 //往返 95 if ($t == 2) 96 { 97 $jie['html'] = $this->fetch('ji2'); 98 }else{ 99 $jie['html'] = $this->fetch('ji'); 100 } 101 // 102 exit(json_encode($jie)); 103 } 104 //单程测试 105 public function jt(){ 106 $json = file_get_contents('mp_3.txt'); 107 $output = json_decode($json,true); 108 109 $j = $this->_out($output,0) ; 110 //循环 Legs 111 112 $total_price_ci = $j['total_price_ci']; 113 114 unset($j['total_price_ci']); 115 116 $this->assign('piao',$j); 117 $this->assign('total_price_ci',$total_price_ci); 118 119 $this->display(); 120 } 121 //往返测试 122 public function jb(){ 123 $json = file_get_contents('mp_1480656765.txt'); 124 $output = json_decode($json,true); 125 126 $j = $this->_outhui($output,0) ; 127 //循环 Legs 128 129 $total_price_ci = $j['total_price_ci']; 130 131 unset($j['total_price_ci']); 132 133 $this->assign('piao',$j); 134 $this->assign('total_price_ci',$total_price_ci); 135 136 $this->display(); 137 } 138 public function showa(){ 139 echo 'showa'; 140 } 141 private function _outhui(&$output,$shu=1){ 142 $j = array(); 143 foreach($output['Itineraries'] as $v){ 144 $tmp['qu']['id'] = $v['OutboundLegId']; 145 $tmp['hui']['id'] = $v['InboundLegId']; 146 $tmp['Itineraries'] = $v; 147 foreach($output['Legs'] as $va){ 148 if ($va['Id'] == $tmp['qu']['id']) 149 { 150 $tmp['qu']['legs'] = $va; 151 } 152 if ($va['Id'] == $tmp['hui']['id']) 153 { 154 $tmp['hui']['legs'] = $va; 155 } 156 } 157 158 foreach($output['Carriers'] as $vb ){ 159 if ($tmp['qu']['legs']['Carriers']['0'] == $vb['Id']) 160 { 161 $tmp['qu']['Carriers'] = $vb; 162 } 163 if ($tmp['hui']['legs']['Carriers']['0'] == $vb['Id']) 164 { 165 $tmp['hui']['Carriers'] = $vb; 166 } 167 } 168 //出发地信息Places 169 foreach($output['Places'] as $vc){ 170 if ($tmp['qu']['legs']['OriginStation'] == $vc['Id']) 171 { 172 $tmp['qu']['palce'] = $vc; 173 } 174 if ($tmp['hui']['legs']['OriginStation'] == $vc['Id']) 175 { 176 $tmp['hui']['palce'] = $vc; 177 } 178 } 179 //目的地信息Places 180 foreach($output['Places'] as $vb){ 181 if ($tmp['qu']['legs']['DestinationStation'] == $vb['Id']) 182 { 183 $tmp['qu']['palce_b'] = $vb; 184 } 185 if ($tmp['hui']['legs']['DestinationStation'] == $vb['Id']) 186 { 187 $tmp['hui']['palce_b'] = $vb; 188 } 189 } 190 191 foreach($tmp['Itineraries']['PricingOptions'] as &$vd){ 192 foreach($output['Agents'] as $va){ 193 if ($vd['Agents'][0] == $va['Id']) 194 { 195 $vd['lai'] = $va; 196 } 197 } 198 } 199 200 $j[] = $tmp; 201 } 202 203 $total_price_ci = 0; 204 foreach($j as &$v){ 205 $total_price_ci += count($v['Itineraries']['PricingOptions']); 206 } 207 //P($j); 208 $j['total_price_ci'] = $total_price_ci ; 209 return $j; 210 } 211 private function _out(&$output,$shu=2){ 212 $j = array(); 213 foreach($output['Legs'] as $v){ 214 $tmp['legs'] = $v; 215 //循环查找 Carriers 216 foreach($output['Carriers'] as $va){ 217 if ($v['Carriers']['0'] == $va['Id']) 218 { 219 $tmp['Carriers'] = $va; 220 } 221 } 222 223 //循环查找 Itineraries 224 foreach($output['Itineraries'] as $vi){ 225 if ($v['Id'] == $vi['OutboundLegId']) 226 { 227 $tmp['Itineraries'] = $vi; 228 } 229 } 230 231 //出发地信息Places 232 foreach($output['Places'] as $va){ 233 if ($v['OriginStation'] == $va['Id']) 234 { 235 $tmp['from'] = $va; 236 } 237 } 238 //目的地信息Places 239 foreach($output['Places'] as $vb){ 240 if ($v['DestinationStation'] == $vb['Id']) 241 { 242 $tmp['to'] = $vb; 243 } 244 } 245 246 if (count($tmp['Itineraries']['PricingOptions']) > $shu) 247 { 248 $j[]= $tmp; 249 } 250 251 252 } 253 254 $total_price_ci = 0; 255 foreach($j as &$v){ 256 $total_price_ci += count($v['Itineraries']['PricingOptions']); 257 foreach($v['Itineraries']['PricingOptions'] as &$vb){ 258 foreach($output['Agents'] as $va){ 259 if ($vb['Agents'][0] == $va['Id']) 260 { 261 $vb['lai'] = $va; 262 } 263 } 264 } 265 } 266 //P($j); 267 $j['total_price_ci'] = $total_price_ci ; 268 return $j; 269 } 270 }
演示网址:
http://lv.226660.pw/index.php/fly/index.html