您好,欢迎访问一九零五行业门户网

怎么调用外网接口

现在想调用同程旅游的机票数据,该如何调用?
接口文档发给我没看懂,本人是做前端的,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 发生改变或者通信格式发生改变,只需要更改后台和第三方交互的部分,对前端毫无感知,这样无论从开发还是升级都是更方便的
其它类似信息

推荐信息