php跨域json請求,php跨域請求解決方案

本文目錄一覽:

PHP跨域獲取json數據的方法,PHP裡面有沒有類似ajax的函數?

php中的文件讀寫函數基本上都可直接支持url,也就是說你可以像操作本地文件一樣直接操作其他網站的文件(當然只是讀取,寫入是不可能的),而且沒有任何跨域限制,比如下面一行代碼就可直接讀取百度首頁的html代碼:

$bd=file_get_contents(“”);

如果想獲取json數據,只需把網址換一下即可。當然,還需要做一下格式轉換,php本身就有專門的json轉換函數:

$json=json_decode(file_get_contents(“網址”),true);

這樣一看,是不是比前端的ajax還要簡單百倍?!

jsonp跨域請求範例,求PHP版本的jsonp範例。

jquery代碼:

$.getJSON(“;callback=?”,{id: 10, name: “test”}, function(data){

alert(data.msg);

});

服務端返回:

jsonp1310628945031({“rs”:true,”msg”:”u60a8u7684u4fe1u606fu63d0u4ea4u6210u529fuff01″})

PHP代碼:

$result[‘rs’] = false;

$result[‘msg’] = ‘您的信息提交成功!’;

$json = new Services_JSON();

header(‘Content-Type: application/json’);

echo $_GET[‘callback’].'(‘.$json-encode($result).’)’;

php怎麼配合$getjson跨域callback=

type : “post”,

url : “ajax.php”,

dataType : “jsonp”,

jsonp: “callback”,//傳遞給請求處理程序或頁面的,用以獲得jsonp回調函數名的參數名(默認為:callback)

jsonpCallback:”success_jsonpCallback”,//自定義的jsonp回調函數名稱,默認為jQuery自動生成的隨機函數名

success : function(json){

alert(‘success’);

},

error:function(){

alert(‘fail’);

}

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 = 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’);

PHP如何實現跨域傳遞參數

通常是用json,你可以用php的函數json_encode(),轉換為json格式,然後輸出進行傳遞

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/192369.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-01 09:57
下一篇 2024-12-01 09:57

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • docker-ce-18.03.1.ce-1.el7.centos.x86_64需要pigz這個依賴的解決方案

    當我們在linux centos系統中安裝docker-ce-18.03.1.ce-1.el7.centos.x86_64時,有時可能會遇到「nothing provides pi…

    編程 2025-04-29
  • IDEA Java發送郵件出現錯誤解決方案

    IDEA Java是一款常用的Java開發工具,很多開發者都使用它來開發Java應用程序。然而,在使用IDEA Java發送郵件時,有可能會出現一些錯誤。本文將從多個方面對該錯誤進…

    編程 2025-04-29
  • 光模塊異常,SFP未認證(entityphysicalindex=6743835)——解決方案和

    如果您遇到類似optical module exception, sfp is not certified. (entityphysicalindex=6743835)的問題,那麼…

    編程 2025-04-29
  • 打包後頁面空白的解決方案

    當我們在調試階段時,我們的app可能看起來完美無缺,但當我們進行打包時,在運行app時,我們可能會遇到白屏或空白的問題。在這篇文章中,我們將探討如何解決這種問題。 一、檢查文件路徑…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • JSON的MD5

    在Web開發過程中,JSON(JavaScript Object Notation)是最常用的數據格式之一。MD5(Message-Digest Algorithm 5)是一種常用…

    編程 2025-04-29
  • 使用Java將JSON寫入HDFS

    本篇文章將從以下幾個方面詳細闡述Java將JSON寫入HDFS的方法: 一、HDFS簡介 首先,先來了解一下Hadoop分散式文件系統(HDFS)。HDFS是一個可擴展性高的分散式…

    編程 2025-04-29
  • Qttus:一站式的物聯網解決方案

    Qttus 是一個全面的物聯網(IoT)解決方案,用於連接感測器、設備和雲。它可以幫助您在現有商業和製造業應用程序中輕鬆地添加 IoT 功能,同時提供可伸縮且安全的數據傳輸和存儲。…

    編程 2025-04-29
  • Python折扣問題解決方案

    Python的折扣問題是在計算購物車價值時常見的問題。在計算時,需要將原價和折扣價相加以得出最終的價值。本文將從多個方面介紹Python的折扣問題,並提供相應的解決方案。 一、Py…

    編程 2025-04-28

發表回復

登錄後才能評論