現在想調用同程旅遊的機票資料,該如何調用?
介面文檔發給我沒看懂,本人是做前端的,php不太瞭解
回複內容:
現在想調用同程旅遊的機票資料,該如何調用?
介面文檔發給我沒看懂,本人是做前端的,php不太瞭解
幕後處理的話curl 或者 fsockopen發http請求,前端處理的花就如 @baofan 所說ajax請求。
貼個代碼
protected function post($url, $post_data = '', $trade_type='APP', $certificate = false, $timeout = 30){ $ch = curl_init(); //設定逾時 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //如果有配置代理這裡就設定代理 if($this->config['CURL_PROXY_HOST'] != "0.0.0.0" && $this->config['CURL_PROXY_PORT'] != 0){ curl_setopt($ch,CURLOPT_PROXY, $this->config['CURL_PROXY_HOST']); curl_setopt($ch,CURLOPT_PROXYPORT, $this->config['CURL_PROXY_PORT']); } curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校正 //設定header curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch,CURLOPT_CAINFO, $this->config['ssl_public_path']); if($certificate == true){ //設定認證 //使用認證:cert 與 key 分別屬於兩個.pem檔案 if (strtoupper($trade_type) == "APP"){ curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT, $this->config['open']['ssl_cert_path']); curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY, $this->config['open']['ssl_key_path']); }else{ curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT, $this->config['mp']['ssl_cert_path']); curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY, $this->config['mp']['ssl_key_path']); } } //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //運行curl $data = curl_exec($ch); //返回結果 if($data){ curl_close($ch); return $data; } else { curl_close($ch); return "xxx"; } }
再貼個fsockopen的,這個是為了非同步,所以沒接收資料.
public function async_post($host,$path,$data){ $post = http_build_query($data); $len = strlen($post); $fp = @fsockopen( $host , 80, $errno, $errstr, 5); if (!$fp) { return false; } else { $out = "POST $path HTTP/1.1\r\n"; $out .= "Host: $host\r\n"; $out .= "Content-type: application/x-www-form-urlencoded\r\n"; $out .= "Connection: Close\r\n"; $out .= "Content-Length: $len\r\n"; $out .= "\r\n"; $out .= $post."\r\n"; fwrite($fp, $out); fclose($fp); return true; } }
發http請求唄,js不是有ajax嘛
ajax有跨域可以用jsonp
PHP有CURL
認為這種機票資料最好應該在幕後處理,前台和後台確定好格式後,對前端就只是一個展示。原因是若第三方的介面url 發生改變或者通訊格式發生改變,只需要更改後台和第三方互動的部分,對前端毫無感知,這樣無論從開發還是升級都是更方便的