1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 用PHP做服务器转发层 解决js的ajax跨域访问问题

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

时间:2018-10-23 06:55:12

相关推荐

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

php教程|php手册

用,PHP,做,服务器,转发,层,解决,ajax,跨域,访问,问题,做,ajax,应,用时,会,遇到,你,需要,

php教程-php手册

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

微测试网站源码,ubuntu系统图像,tomcat导入数据库,飞龙与爬虫,rn php,南城seo优化关键词哪家好lzw

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

小偷采集网站源码,ubuntu安装字体包,tomcat无法打开本地目录,爬虫 邮箱地址,php基础教程pdf学习教程,莆田外贸seolzw

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

源码管理软件,vscode怎么打开问题,ubuntu服务重启,tomcat线程执行,c 修改sqlite3,分布式爬虫百科,php 生成txt并下载,辉县seo搜索优化,家电维修网站模版,虾米网页播放器代码, 微信模板开发lzw

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

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

[php]

<?php

/**

* ajax业务处理中的接口转发层,解决ajax跨域访问的问题

* 工作原理:问请求通过本程序做中转,在本地服务器层完成与远程服务接口的交互

* 备注:使用时 URL_ROOT 这个参数需要根据你的目标接口地址进行修改,本转发层之能用于单接口的Web Service接口服务

* 程序支持POST数据与GET数量的同时转发;

* @version 1.0.0.2

* @author JerryLi lijian@dzs.mobi

* @copyright b.dzs.mobi -11-16

* */

class interface_relay

{

/**接口根地址(此处是需要修改的地方)*/

const URL_ROOT = ‘http://api.air-/InterFace/’;

/**字符集*/

const CHARSET = ‘UTF-8’;

/**GET*/

private $msGets = ”;

/**POST*/

private $maGetPostData = array();

function __construct()

{

$this->getPOST();

$this->getGET();

if($this->msGets != ” || count($this->maGetPostData) > 0)

{ //存在输入数据

if(count($this->msGets) > 0)

$sUrl = self::URL_ROOT .’?’. $this->msGets;

else

$sUrl = self::URL_ROOT;

header(‘Content-Type: text/html; charset=’. self::CHARSET);

echo $this->getContent($sUrl);

}

else

{

header(‘Content-Type: text/html; charset=’. self::CHARSET);

echo $this->getContent(self::URL_ROOT);

}

}

function __destruct()

{

unset($maGetPostData, $msGets);

}

/**

* 载入POST数据

* @return bool

* */

private function getPOST()

{

$handle = @fopen(‘php://input’, ‘r’);

$data = ”;

do

{

$data = @fread($handle, 1024);

if (strlen($data) == 0)

break;

else

$this->maGetPostData[] = $data;

}while(true);

fclose($handle);

unset($data, $handle);

return count($this->maGetPostData) >= 1;

}

/**

* 载入GET数据

* @return bool

* */

private function getGET()

{

/*取得GET内容*/

if (count($_GET) > 0)

{

$aTmp = array();

foreach ($_GET as $sKey => $sVal)

$aTmp[] = $sKey .’=’. urlencode($sVal);

$this->msGets = implode(‘&’, $aTmp);

return true;

}

else

return false;

}

/**

* 读取远程接口返回的内容

* @return string

* */

private function getContent($sGetUrl)

{

/**/

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $sGetUrl); //设置GET的URL地址

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);//将结果保存成字符串

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);//连接超时时间s

curl_setopt ($ch, CURLOPT_TIMEOUT, 10);//执行超时时间s

curl_setopt ($ch, CURLOPT_DNS_CACHE_TIMEOUT, 1800);//DNS解析缓存保存时间半小时

curl_setopt($ch, CURLOPT_HEADER,0);//丢掉头信息

if (count($this->maGetPostData) > 0)

{ //存在POST数据需要提交

curl_setopt($ch, CURLOPT_POST, 1); //启用POST数据

curl_setopt($ch, CURLOPT_POSTFIELDS, implode(”, $this->maGetPostData));//提交POST数据

}

$sData = curl_exec($ch);

curl_close($ch);

unset($ch);

return $sData;

}

}

$o = new interface_relay();

unset($o);

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。