跨域获取数据php php处理跨域请求

thinkphp 跨域获取 xml 转 json最简单的转换:
function simplest_xml_to_array($xmlstring) {
return json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);
}
完整点的:
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); #
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = $xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = https://www.04ip.com/post/array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr = $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag 'tag'
$parent[$level-1] = $current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = $current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level];
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = $current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line 'tag /'
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level];
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag '/tag'
$current = $parent[$level-1];
}
}
return($xml_array);
}
?
函数描述及例子
$arr = xml2array(file_get_contents("tools.xml"),1,'attribute');
使用jquery的ajax的jsonp类型跨域请求数据、始终获取不到返回数据呢使用jquery的ajax的jsonp类型跨域请求数据、始终获取不到返回数据是设置错误造成的,解决方法为:
1、创建基本的文件结构json_ajax.html和json_ajax.php,下载jquery.js 。
2、如图分别编写json_ajax.html和json_ajax.php文件的编码 。
3、分别在w(l)amp环境下运行json_ajax.html和json_ajax.php 。
4、经过源码和运行结果的分析,知道了在json_ajax.html中设置了按钮的点击事件,点击按钮 。
注意事项:
jQuery的核心特性可以总结为:具有独特的链式语法和短小清晰的多功能接口;具有高效灵活的css选择器 , 并且可对CSS选择器进行扩展;拥有便捷的插件扩展机制和丰富的插件 。jQuery兼容各种主流浏览器,如IE6.0 、FF1.5 、Safari2.0 、Opera9.0 等 。
PHP中运用jQuery的Ajax跨域调用实现代码可以在页面定义一个调用方法跨域获取数据php,如下跨域获取数据php:
复制代码
代码如下:
function
getData(){
$.getJSON("?",
{
"m":"data",//
指定php跨域获取数据php的文件名字
"act":"getdata",//
指定php文件中的方法
"name":"问题儿童"//
传入的参数
},
function(data)
{
//
获得返回值
}
});
}
对应链接下(123.123.123.123)的PHP文件,一般默认先调用index.php文件,通过index.php文件内的方法处理后,转到对应的php文件,并且找到对应的方法,执行之 。
index.php代码如下跨域获取数据php:
复制代码
代码如下:
?php
/**
*
入口文件
*/
$string
=
$_SERVER["REQUEST_URI"];//
获取访问的url
$m
=
get_m($string);
$file_path
=
"app/".$m.".php";
define('IS_INDEX',true);//
阻止直接访问app目录
require
($file_path);
/**
*
*
获取访问php文件
*
@param
string
$url
*/
function
get_m($url){
$strings
=
explode('m=',
$url);
$res
=
explode("",
$strings[1]);
return
empty($res[0])?'index':$res[0];
}
?
data.php代码如下:
复制代码
代码如下:
?php
/**
*
data文件
*/
$act
=
!empty($_GET['act'])
?
$_GET['act']
:
'';
if
($act
==
'getdata')
{
$name
=
"跨域获取数据php我的名字叫:".$_REQUEST['name'];
echo
$_REQUEST["callback"]."(".json_encode($name).")";
}
?
成功调用后,画面就能获取到返回的json数据了 。
跨域是指什么,因为什么引起的?有哪些解决方案?web前端知识域(Domain)是Windows网络中独立运行的单位,域之间相互访问则需要建立信任关系(即Trust Relation) 。信任关系是连接在域与域之间的桥梁 。当一个域与其他域建立了信任关系后 , 2个域之间不但可以按需要相互进行管理,还可以跨网分配文件和打印机等设备资源,使不同的域之间实现网络资源的共享与管理 。有一种简明的说法来解释广域跨域:跨域访问,简单来说就是 A 网站的 javascript 代码试图访问 B 网站,包括提交内容和获取内容 。由于安全原因,跨域访问是被各大浏览器所默认禁止的 。
解决方案:
1、js向服务器发送请求,然后让服务器去另一个域上获取数据后返回 。(用于你无法控制另一个域) 比如php中利用cUrl 。
2、放置跨域文件.
3、用JSONP 。虽然不能跨域进行通信,但是可以引入跨域的js文件 。
先定义一个函数
var returnData;
function getData(obj) {
returnData = https://www.04ip.com/post/JSON.parse(obj);
}
当我们要向请求数据的时候,我们可以引入某个包含返回信息的js文件 。
比如: script type="text/javascript" src="" /
js的内容是getData({json:'格式'});返回时输出 格式是 text/javascript (比如php用header('Content-type:text/javascript');来输出)
那么文件加载好后解析js时就会执行这个函数 , 返回得到的数据就被赋值给了returnData变量
以这样的方式插入到页面中:
var script = document.createElement('script');
script.src='';//传入参数id=12321
body.appendChild(script);
$.getJSON跨域获取数据问题使用jsonp
$.ajax({
url : ';output=jsonak=rnm8udmHdWaHFWZTO2tuTiG8',
dataType : 'jsonp',
success : function(json){
//your code
}
});
【跨域获取数据php php处理跨域请求】跨域获取数据php的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php处理跨域请求、跨域获取数据php的信息别忘了在本站进行查找喔 。

    推荐阅读