PHP-implemented simple mockjson script sharing, phpmockjson script _ PHP Tutorial

Source: Internet
Author: User
PHP implements simple mockjson script sharing and phpmockjson script. The simple mockjson script implemented by PHP is shared. there are too many methods for phpmockjson script to go to mock. However, when you still need to connect to a remote server to test the true returned results, if PHP implements simple mock json script sharing on the machine, phpmockjson script

There are too many methods to go to mock now, but when you still need to connect to the remote server to test the true return, if the node is not run on the machine and php is available, use this product to temporarily mock it, it may be easier.

The code is as follows:


<? Php
/**
* 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 field: Cross-Origin field
*/
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' = & gt; 200,
'Desc' => 'get the default data .',
'Login' => true,
'Data' => Array (
'Name' => 'test api .'
)
);
}
Returnjson_encode ($ data );
}


/**
* Output character set, which can return gbk, gb2312, and UTF-8
* If it is invalid or not set, UTF-8 is output.
*
* @ 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 result
*/
Die (jsonGenerator ());

If you don't want to change the data object in php, you may want to change the json directly. why don't I directly access a json file?:

1. you may need a callback to wrap this result;
2. you may expect this json to allow cross-origin requests;
3. you may expect this json to be able to customize the header encoding...

The code is as follows:


{
"Data": 1,
"W": "Test"
}


The code is very simple, but it is not described much.

Share the response json script. phpmockjson script now has too many methods to go to mock. However, if you want to connect to a remote server and test whether the returned result is true...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.