现在想调用同程旅游的机票数据,该如何调用?
接口文档发给我没看懂,本人是做前端的,php不太了解
现在想调用同程旅游的机票数据,该如何调用?
接口文档发给我没看懂,本人是做前端的,php不太了解
后台处理的话curl 或者 fsockopen发http请求,前端处理的花就如 @baofan 所说ajax请求。
贴个代码
<code> 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"; } }</code>
再贴个fsockopen的,这个是为了异步的,所以没接收数据.
<code>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; } }</code>
发http请求呗,js不是有ajax嘛
ajax有跨域可以用jsonp
PHP有CURL
认为这种机票数据最好应该在后台处理,前台和后台确定好格式后,对前端就只是一个展示。原因是若第三方的接口url 发生改变或者通信格式发生改变,只需要更改后台和第三方交互的部分,对前端毫无感知,这样无论从开发还是升级都是更方便的