怎麼調用外網介面

來源:互聯網
上載者:User
現在想調用同程旅遊的機票資料,該如何調用?

介面文檔發給我沒看懂,本人是做前端的,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 發生改變或者通訊格式發生改變,只需要更改後台和第三方互動的部分,對前端毫無感知,這樣無論從開發還是升級都是更方便的

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.