Home > Backend Development > PHP Tutorial > Sharing of simple mock json script implemented in PHP_PHP tutorial

Sharing of simple mock json script implemented in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:07:33
Original
1282 people have browsed it

Sharing of simple mock json scripts implemented in PHP

This article mainly introduces the sharing of simple mock json scripts implemented in PHP. This article directly gives the implementation code. Friends who need it can refer to it. Next

There are too many ways to mock now, but when you still need to connect to a remote server to test the real return, if node is not running on the machine and there is php, it may be easier to use this product to temporarily mock.

The code is as follows:


/**
* Mock Json for Javascript
*
* @author soulteary
* @date 2014-06-15
*/


/**
* Request interface field: character set
*/
define('charset','charset');

/**
* Request interface field: callback function name
*/
define('callback','callback');

/**
* Request interface fields: cross-domain fields
*/
define('crossDomain','cross-domain');


/**
* Output mock data
* If the mock.json file exists, the data is obtained from mock.js
*
* @return string
*/
functionmockData()
{
if(file_exists('mock.json')){
$data=json_decode(file_get_contents('mock.json'));
}else{
$data=Array(
'code'=>200,
'desc'=>'Get the default data.',
'login'=>true,
'data'=>Array(
'name'=>'test api.'
)
);
}
returnjson_encode($data);
}


/**
* Output character set, allowing results to be gbk, gb2312, utf-8
* If illegal or not set, output utf-8
*
* @return string
*/
functioncharset()
{
$ret='utf-8';
if(empty($_REQUEST[charset])){
return$ret;
}else{
$charset=strtolower($_REQUEST[charset]);
if(in_array($charset,array('gbk','gb2312'),true)){
return$charset;
}else{
return$ret;
}
}
}


/**
* Assemble json data
*
* @return string
*/
functionjsonGenerator()
{
if(!empty($_REQUEST[callback])){
header('Content-Type: application/javascript; charset='.charset());
return$_REQUEST[callback]."(".mockData().");";
}else{
if(!empty($_REQUEST[crossDomain])){
header("Access-Control-Allow-Origin: *");
};
header('Content-type: application/json; charset='.charset());
returnmockData();
}
}

/**
* Output results
*/
die(jsonGenerator());

If you don’t want to change the data object in php and find it troublesome, then just change the json directly. You may ask, then why don’t I access a json directly? Answer:

1. You may need a callback to wrap this result;
2. You may expect this json to allow cross-domain requests;
3. You may expect that this json can be customized with header encoding...

Copy the code The code is as follows:


{
"data":1,
"w":"Test"
}
The code is very simple, so I won’t describe it too much.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955833.htmlTechArticleSharing a simple mock json script implemented in PHP. This article mainly introduces the sharing of a simple mock json script implemented in PHP. This article The implementation code is given directly. Friends who need it can refer to it. There are too many now...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template