用PHP做服务器转发层,解决js的ajax跨域访问问题

做js的ajax应用时,会遇到你需要请求的接口并不在你当前域下,此时就会出现跨域访问的问题,浏览器会禁止你请求这个接口。

此时怎么访问这个WebService的接口呢?

一个简单的办法就是在本域的服务器上,增加一个转发层,将浏览器上过来的请求接收后,通过服务器将这个请求转发到对应的WebService上,然后把返回结果再取回后,送回js的请求页面。

一般而言这个是解决跨域访问最安全与最具兼容性的办法。

下面是我写的一个php脚本,可以完成这个转发过程,仅供参考:

[php]  view plain copy
  1. <?php  
  2. /** 
  3.  * ajax业务处理中的接口转发层,解决ajax跨域访问的问题 
  4.  *   工作原理:问请求通过本程序做中转,在本地服务器层完成与远程服务接口的交互 
  5.  *   备注:使用时 URL_ROOT 这个参数需要根据你的目标接口地址进行修改,本转发层之能用于单接口的Web Service接口服务 
  6.  *        程序支持POST数据与GET数量的同时转发; 
  7.  * @version 1.0.0.2 
  8.  * @author JerryLi lijian@dzs.mobi 
  9.  * @copyright b.dzs.mobi 2012-11-16 
  10.  * */  
  11. class interface_relay  
  12. {  
  13.     /**接口根地址(此处是需要修改的地方)*/  
  14.     const URL_ROOT = 'http://api.air-id.net/InterFace/';  
  15.     /**字符集*/  
  16.     const CHARSET = 'UTF-8';  
  17.     /**GET*/  
  18.     private $msGets = '';  
  19.     /**POST*/  
  20.     private $maGetPostData = array();  
  21.   
  22.     function __construct()  
  23.     {  
  24.         $this->getPOST();  
  25.         $this->getGET();  
  26.         if($this->msGets != '' || count($this->maGetPostData) > 0)  
  27.         {   //存在输入数据  
  28.             if(count($this->msGets) > 0)  
  29.                 $sUrl = self::URL_ROOT .'?'$this->msGets;  
  30.             else  
  31.                 $sUrl = self::URL_ROOT;  
  32.             header('Content-Type: text/html; charset='. self::CHARSET);  
  33.             echo $this->getContent($sUrl);  
  34.         }  
  35.         else  
  36.         {  
  37.             header('Content-Type: text/html; charset='. self::CHARSET);  
  38.             echo $this->getContent(self::URL_ROOT);  
  39.         }  
  40.     }  
  41.   
  42.     function __destruct()  
  43.     {  
  44.         unset($maGetPostData$msGets);  
  45.     }  
  46.   
  47.     /** 
  48.      * 载入POST数据 
  49.      * @return bool 
  50.      * */  
  51.     private function getPOST()  
  52.     {  
  53.         $handle = @fopen('php://input''r');  
  54.         $data = '';  
  55.         do  
  56.         {  
  57.             $data = @fread($handle, 1024);  
  58.             if (strlen($data) == 0)  
  59.                 break;  
  60.             else  
  61.                 $this->maGetPostData[] = $data;  
  62.         }while(true);  
  63.         fclose($handle);  
  64.         unset($data$handle);  
  65.         return count($this->maGetPostData) >= 1;  
  66.     }  
  67.   
  68.     /** 
  69.      * 载入GET数据 
  70.      * @return bool 
  71.      * */  
  72.     private function getGET()  
  73.     {  
  74.         /*取得GET内容*/  
  75.         if (count($_GET) > 0)  
  76.         {  
  77.             $aTmp = array();  
  78.             foreach ($_GET as $sKey => $sVal)  
  79.                 $aTmp[] = $sKey .'='. urlencode($sVal);  
  80.             $this->msGets = implode('&'$aTmp);  
  81.             return true;  
  82.         }  
  83.         else  
  84.             return false;  
  85.     }  
  86.   
  87.     /** 
  88.      * 读取远程接口返回的内容 
  89.      * @return string 
  90.      * */  
  91.     private function getContent($sGetUrl)  
  92.     {  
  93. /**/  
  94.         $ch = curl_init();  
  95.         curl_setopt ($ch, CURLOPT_URL, $sGetUrl); //设置GET的URL地址  
  96.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);//将结果保存成字符串  
  97.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);//连接超时时间s  
  98.         curl_setopt ($ch, CURLOPT_TIMEOUT, 10);//执行超时时间s  
  99.         curl_setopt ($ch, CURLOPT_DNS_CACHE_TIMEOUT, 1800);//DNS解析缓存保存时间半小时  
  100.         curl_setopt($ch, CURLOPT_HEADER,0);//丢掉头信息  
  101.         if (count($this->maGetPostData) > 0)  
  102.         {   //存在POST数据需要提交  
  103.             curl_setopt($ch, CURLOPT_POST, 1); //启用POST数据  
  104.             curl_setopt($ch, CURLOPT_POSTFIELDS, implode(''$this->maGetPostData));//提交POST数据  
  105.         }  
  106.         $sData = curl_exec($ch);  
  107.         curl_close($ch);  
  108.         unset($ch);  
  109.         return $sData;  
  110.     }  
  111. }  
  112.   
  113. $o = new interface_relay();  
  114. unset($o);  
  115. ?>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值