Home > Backend Development > PHP Tutorial > Sharing of simple mock json script implemented in PHP, phpmockjson script_PHP tutorial

Sharing of simple mock json script implemented in PHP, phpmockjson script_PHP tutorial

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

Sharing of simple mock json script implemented in PHP, phpmockjson script

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.

Copy code 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, allowed results are 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 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/955975.htmlTechArticleSharing of simple mock json script implemented in PHP. There are too many ways to mock the phpmockjson script now, but you still need to connect When testing the real return on the remote server, if the machine...
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