這篇文章主要介紹了php跨服務器訪問方法,實例總結了常見的php跨服務器訪問技巧,非常簡單實用,需要的朋友可以參考下
本文實例總結了php跨服務器訪問方法。分享給大家供大家參考。具體分析如下:
近來項目中遇到跨服務器訪問的問題,研究了好些日子,總結如下:
1、用file_get_contents方法
- $host = 'url';
- $randomNumber=file_get_contents($host);
- echo $$randomNumber;
2、用Curl
- $host = 'url';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $host);
- // 返回結果
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- // 使用POST提交
- curl_setopt($ch, CURLOPT_POST, 1);
- // POST參數
- $str = array('a=1','b=2','c=3');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
- // 結果
- $res = curl_exec($ch);
- curl_close($ch);
使用curl庫,使用curl庫之前,你可能需要查看一下php.ini,查看是否已經打開了curl擴展
3、 用fopen打開url, 以get方式獲取內容
- <?php
- $url="http://www.49028c.com/";
- $fp=fopen($url,'r');
- while(!feof($fp)){
- $result.=fgets($fp,1024);
- }
- echo" $result";
- fclose($fp);
- ?>
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答