openresty+yii2.0 development of high Performance RESTFULAPI Series 3: Developing PHP-based RESTFULAPI

Source: Internet
Author: User

1. Configuring Access URLs in nginx.conf

When you access api1.yingtrader.com/boquote, the boquote.php code is executed.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/3A/wKiom1dOs3XisFbWAAD-ptQhK4g266.png "title=" Picture 10.png "alt=" Wkiom1dos3xisfbwaad-ptqhk4g266.png "/>

2. Nginx to implement AJAX cross-domain requests

To enable cross-domain requests on Nginx , you need to add the add_header access-control* directive.

As shown below:

location/{

Add_header ' access-control-allow-origin ' http://other.subdomain.com ';

Add_header ' access-control-allow-credentials ' true ';

Add_header ' Access-control-allow-methods ' POST, GET, OPTIONS, PUT, DELETE, HEAD ';

...

...

The rest of your configuration here

...

...

}

Comments are as follows:

First instruction: Authorizing requests from other.subdomain.com

Second instruction: When the flag is true, responds to whether the request can be exposed

The third day directive: Specifies the method of the request, which can be get,POST , etc.

If you need to allow access from any domain, you can configure this:

Add_header ' Access-control-allow-origin ' *;

Reference: http://www.ttlsa.com/nginx/how-to-allow-cross-domain-ajax-requests-on-nginx/

3. writing the PHP code

get POST parameters via $_post

Error_reporting (e_all^e_notice^e_warning);

... .. ... ..

$login = $_post[' UserAccount ');

$volume = $_post[' volume ');

... .. ... ..

return JSON data

... .. ... ..

$ret = Array ();

if (' OK ' = = $resarr [0]) {

$ret = [' success ' = ' tradesuccess '];

}else{

$ret = [' Error ' = ' tradefail '];

}

echo Json_encode ($ret);


This article is from the "Programming Art" blog, so be sure to keep this source http://itsart.blog.51cto.com/1005243/1785263

openresty+yii2.0 development of high Performance RESTFULAPI Series 3: Developing PHP-based RESTFULAPI

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.