Using PHP as the server forwarding layer to solve ajax cross-origin access problems in JavaScript _ PHP Tutorial

Source: Internet
Author: User
Use PHP as the server forwarding layer to solve ajax cross-origin access problems in JavaScript. When you create a js ajax application, the interface you need to request is not in your current domain. in this case, the cross-origin access problem occurs, and the browser will forbid you to request this interface. In this case, when accessing JavaScript ajax applications, you will encounter an interface that you need to request not in your current domain. in this case, cross-origin access will occur, the browser will forbid you from requesting this interface.

How can I access this WebService interface?

A simple method is to add a forwarding layer on the server of the local domain. after receiving the request from the browser, the server forwards the request to the corresponding WebService, then retrieve the returned results and return them to the js request page.

Generally, this is the most secure and compatible method for cross-origin access.

The following is a php script I wrote to complete the forwarding process for your reference only:

[Php]
/**
* The interface forwarding layer in ajax business processing solves the problem of cross-origin ajax access.
* Working principle: requests are transferred through this program, and interaction with remote service interfaces is completed at the local server layer.
* Note: During use, the URL_ROOT parameter needs to be modified based on your target interface address. This forwarding layer can be used for the Web Service interface Service of a single interface.
* The program supports simultaneous forwarding of POST data and GET data;
* @ Version 1.0.0.2
* @ Author JerryLi lijian@dzs.mobi
* @ Copyright B .dzs.mobi 2012-11-16
**/
Class interface_relay
{
/** Interface root address (here is the place to be modified )*/
Const URL_ROOT = 'http: // api.air-id.net/interface /';
/** Character set */
Const CHARSET = 'utf-8 ';
/** GET */
Private $ msGets = '';
/** POST */
Private $ maGetPostData = array ();

Function _ construct ()
{
$ This-> getPOST ();
$ This-> getGET ();
If ($ this-> msGets! = ''| Count ($ this-> maGetPostData)> 0)
{// Input data exists
If (count ($ this-> msGets)> 0)
$ SUrl = self: URL_ROOT .'? '. $ This-> msGets;
Else
$ SUrl = self: URL_ROOT;
Header ('content-Type: text/html; charset = '. self: CHARSET );
Echo $ this-> getContent ($ sUrl );
}
Else
{
Header ('content-Type: text/html; charset = '. self: CHARSET );
Echo $ this-> getContent (self: URL_ROOT );
}
}

Function _ destruct ()
{
Unset ($ maGetPostData, $ msGets );
}

/**
* Load POST data
* @ Return bool
**/
Private function getPOST ()
{
$ Handle = @ fopen ('php: // input', 'r ');
$ Data = '';
Do
{
$ Data = @ fread ($ handle, 1024 );
If (strlen ($ data) = 0)
Break;
Else
$ This-> maGetPostData [] = $ data;
} While (true );
Fclose ($ handle );
Unset ($ data, $ handle );
Return count ($ this-> maGetPostData)> = 1;
}

/**
* Load GET data
* @ Return bool
**/
Private function getGET ()
{Www.2cto.com
/* GET content */
If (count ($ _ GET)> 0)
{
$ ATmp = array ();
Foreach ($ _ GET as $ sKey => $ sVal)
$ ATmp [] = $ sKey. '='. urlencode ($ sVal );
$ This-> msGets = implode ('&', $ aTmp );
Return true;
}
Else
Return false;
}

/**
* Read the content returned by the remote interface
* @ Return string
**/
Private function getContent ($ sGetUrl)
{
/**/
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ sGetUrl); // you can specify the get url.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // Save the result as a string
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10); // connection timeout s
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // execution timeout s
Curl_setopt ($ ch, CURLOPT_DNS_CACHE_TIMEOUT, 1800); // The DNS resolution cache is saved for half an hour.
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // discard the header information
If (count ($ this-> maGetPostData)> 0)
{// The POST data must be submitted.
Curl_setopt ($ ch, CURLOPT_POST, 1); // enable POST data
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, implode ('', $ this-> maGetPostData); // submit POST data
}
$ SData = curl_exec ($ ch );
Curl_close ($ ch );
Unset ($ ch );
Return $ sData;
}
}

$ O = new interface_relay ();
Unset ($ o );
?>

Bytes. How to access...

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.