JSON:
public function ttt_json(){
$result = array("method" => null);
if($_SERVER['REQUEST_METHOD'] == 'GET' ? true : false){
$result = array("method" => "GET","data" => $_GET['data']);
}
if($_SERVER['REQUEST_METHOD'] == 'POST' ? true : false){
$result = array("method" => "POST","data" => $_POST['data']);
}
if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
$result = array("method" => "AJAX");
}
return json_encode($result);
}
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function postJson($url,$array=[]){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
$post_data = $array;
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
var_dump(getJson("http://www.***.com/index.php/test/temp/ttt_json?data=nihao"));
var_dump(postJson("http://www.***.com/index.php/test/temp/ttt_json",["data"=>"nihao"]));
XML:
function input_xml_json(){
$xmldata = file_get_contents("php://input");
$xmlResult = simplexml_load_string($xmldata);
return json_encode($xmlResult);
}
function log($file,$txt){
$fp = fopen($file,'ab+');
fwrite($fp,'-----------'.date('Y-m-d H:i:s').'-----------------'."\n");
fwrite($fp,$txt);
fwrite($fp,"\r\n\r\n\r\n");
fclose($fp);
}
function getxml($url,$xml){
$post_url = $url;
$post_str = $xml;
$timeout = 10;
$ch = curl_init();
$header[] = "Content-type: text/xml";
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$xml="<apps>
<app>
<id>1</id>
<name>Google Maps</name>
<virsion>1.0</virsion>
</app>
<app>
<id>2</id>
<name>Chrome</name>
<version>2.1</version>
</app>
<app>
<id>3</id>
<name>Google Play</name>
<version>2.3</version>
</app>
</apps>";
var_dump(getxml("http://www.xxx.com/index.php/test/temp/input_xml_json",trim($xml)));
yaml: 两种方式 1、使用pecl的yaml扩展,https://pecl.php.net/package/yaml ,需要编译安装,稍显麻烦,但是性能比第二种好一些。
2、使用composer包symfony/yaml,安装只需要 composer require symfony/yaml ,都是php写的,直接安装即可。
1:
选择最新扩展,下载正在使用php版本,将php_yaml.dll 扩展放到php的ext下 重启,-》php -m php-yaml的使用:
function get_yaml_json2(){
try {
$data = yaml_parse_file('./db.yaml');
} catch (ParseException $e) {
echo $e->getMessage();
}
return json_encode($data, true);
}
var_dump(get_yaml_json2());
2:
composer require symfony/yaml
命令下后会有vendor文件
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
function get_yaml_json(){
try {
$str_yaml = 'database:
host: 127.0.0.1
user: root
dbname: test
pwd: 123456';
$data = Yaml::parse($str_yaml);
} catch (ParseException $e) {
echo $e->getMessage();
}
return json_encode($data, true);
}
var_dump(get_yaml_json());
二进制传输数据
$str = '{"code": 1,"msg": "ok","time": "1637998234","data": {"is_tc": 1,"advertisement": [{"id": 5,"content": "书(拼音:shū),是汉语通用规范一级字 [1] 。最早见于甲骨文 [2] 。本义作动词,是书写、记述的意思;后引申为名词,指简册、典籍、文书、信函等。","type": "1","status": "1","start_time": 1637745813,"end_time": 1638000208,"weigh": 2}]}}';
function StrToBin($str){
$arr = preg_split('/(?<!^)(?!$)/u', $str);
foreach($arr as &$v){
$temp = unpack('H*', $v);
$v = base_convert($temp[1], 16, 2);
unset($temp);
}
return join(' ',$arr);
}
$binary = StrToBin($str);
function BinToStr($str){
$arr = explode(' ', $str);
foreach($arr as &$v){
$v = pack("H".strlen(base_convert($v, 2, 16)), base_convert($v, 2, 16));
}
return join('', $arr);
}
var_dump(json_decode(BinToStr($binary),true));
|